Reset a CoreData persistent store
Basically, what I'm trying to do is to wipe out all data in my CoreData persistent store, then import new data. How would you do this? It seems that the simplest solution is call [NSPersistentStoreCoordinator removePersistentStore:error:]
and then remove the file. Is that the best practice available? Is it thread-safe?
Thank you very much,
#
Question 0.1: was
I am trying to update the data in a CoreData persistent store. My user is seeing a table view with statistical data. I want to update the application by deleting all existing data, then importing new data. I would like to s开发者_JS百科how a progress view to tell the user that the application is not hanging.
I have added the following resetPersistentStore
method in my AppDelegate (persistentStoreCoordinator
is given for reference):
// ...
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
// ...
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kPersistentStoreFilename]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
/**
* Will remove the persistent store
*/
- (NSPersistentStoreCoordinator *)resetPersistentStore {
NSError *error;
[managedObjectContext lock];
// FIXME: dirty. If there are many stores...
NSPersistentStore *store = [[persistentStoreCoordinator persistentStores] objectAtIndex:0];
if (![persistentStoreCoordinator removePersistentStore:store error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Delete file
if (![[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Delete the reference to non-existing store
[persistentStoreCoordinator release];
persistentStoreCoordinator = nil;
NSPersistentStoreCoordinator *r = [self persistentStoreCoordinator];
[managedObjectContext unlock];
return r;
}
Then in my view I do (in another thread since I am using MBProgressHUD
:
PatrimoineAppDelegate *appDelegate = (PatrimoineAppDelegate *)[[UIApplication sharedApplication] delegate];
// Delete everything
[appDelegate resetPersistentStore];
And I get an EXC_BAD_ACESS
...
I do not know CoreData or multithreading very well, maybe I'm doing an evident error...
For those attempting this on iOS9+, there are now the destroyPersistentStoreAtURL
and replacePersistentStoreAtURL
APIs.
If your goal is to empty the data store and reload it with new information, you may be better off using NSManagedObjectContext's reset
and then loading in new data.
From NSManagedObjectContext's documentation
A context always has a “parent” persistent store coordinator which provides the model and dispatches requests to the various persistent stores containing the data. Without a coordinator, a context is not fully functional. The context’s coordinator provides the managed object model and handles persistency. All objects fetched from an external store are registered in a context together with a global identifier (an instance of NSManagedObjectID) that’s used to uniquely identify each object to the external store.
Removing the persistent store and using the managed object context associated with the store is probably the cause of the error.
Still not work! Abort caused by ManagedObjectContext link with invalid persistance store. Finally it work if I delete the ManagedObjectContext and let the app re-create later
Here is my modification
- (NSPersistentStoreCoordinator *)resetPersistentStore
{
NSError *error = nil;
if ([persistentStoreCoordinator_ persistentStores] == nil)
return [self persistentStoreCoordinator];
[managedObjectContext_ release];
managedObjectContext_ = nil;
// FIXME: dirty. If there are many stores...
NSPersistentStore *store = [[persistentStoreCoordinator_ persistentStores] lastObject];
if (![persistentStoreCoordinator_ removePersistentStore:store error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Delete file
if ([[NSFileManager defaultManager] fileExistsAtPath:store.URL.path]) {
if (![[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
// Delete the reference to non-existing store
[persistentStoreCoordinator_ release];
persistentStoreCoordinator_ = nil;
NSPersistentStoreCoordinator *r = [self persistentStoreCoordinator];
return r;
}
Here is the solution. There may be some more elegant options (lock...) but this one works.
/**
* Will remove the persistent store
*/
- (NSPersistentStoreCoordinator *)resetPersistentStore {
NSError *error = nil;
if ([persistentStoreCoordinator persistentStores] == nil)
return [self persistentStoreCoordinator];
[managedObjectContext reset];
[managedObjectContext lock];
// FIXME: dirty. If there are many stores...
NSPersistentStore *store = [[persistentStoreCoordinator persistentStores] lastObject];
if (![persistentStoreCoordinator removePersistentStore:store error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Delete file
if ([[NSFileManager defaultManager] fileExistsAtPath:store.URL.path]) {
if (![[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
// Delete the reference to non-existing store
[persistentStoreCoordinator release];
persistentStoreCoordinator = nil;
NSPersistentStoreCoordinator *r = [self persistentStoreCoordinator];
[managedObjectContext unlock];
return r;
}
You can swap out (or delete) a persistent store and then reset the context (just make sure you refetch any objects you have in memory):
for (NSPersistentStore *store in persistentStoreCoordinator.persistentStores) {
removed = [persistentStoreCoordinator removePersistentStore:store error:nil];
}
// You could delete the store here instead of replacing it if you want to start from scratch
[[NSFileManager defaultManager] replaceItemAtURL:storeURL
withItemAtURL:newStoreURL
backupItemName:nil
options:0
resultingItemURL:nil
error:nil];
NSPersistentStore *persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];
[myContext reset];
I'm using this to perform an import into a temporary store on a private queue and then overwriting the main thread store and reloading everything when I'm finished. I was importing into a child context but deleting existing objects without side effects became troublesome in my case.
Before resetting the persistentStore you must first reset all the managedObjectContext associated with that or else all the managedObjects will not have context to be accessed this may cause error.
it is good to always remove the sqlite file directly from file system and set managedObjectContext and persistentStoreCoordinator to nil, rather than calling removePersistentStore. This will recreate the persistantStore and managedObjectContext next time you try to access or start storing.
I found the easiest way to deal with these types of problems, so long as you can reload your Core Data data, is to follow these steps:
(1) Reset the simulator.
(2) Delete the sqlite database from your project.
(3) Delete the simulator directory from your machine.
Surprisingly I found that doing steps 1 & 2 did not always work without step 3.
You will need to reload your Core Data store if you do this.
精彩评论