Delete/Reset all entries in Core Data?

后端 未结 30 3151
天命终不由人
天命终不由人 2020-11-22 16:00

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.


Edit

30条回答
  •  时光取名叫无心
    2020-11-22 16:15

    I've written a clearStores method that goes through every store and delete it both from the coordinator and the filesystem (error handling left aside):

    NSArray *stores = [persistentStoreCoordinator persistentStores];
    
    for(NSPersistentStore *store in stores) {
        [persistentStoreCoordinator removePersistentStore:store error:nil];
        [[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
    }
    
    [persistentStoreCoordinator release], persistentStoreCoordinator = nil;
    

    This method is inside a coreDataHelper class that takes care of (among other things) creating the persistentStore when it's nil.

提交回复
热议问题