How to remove persistent master data storage

I need to delete my persistent storage (making it an object by object is impractical because I have more than 100,000 objects). I tried this:

- (IBAction)resetDatabase:(id)sender { NSPersistentStore* store = [[__persistentStoreCoordinator persistentStores] lastObject]; NSError *error = nil; NSURL *storeURL = store.URL; // release context and model [__managedObjectContext release]; [__managedObjectModel release]; __managedObjectModel = nil; __managedObjectContext = nil; [__persistentStoreCoordinator removePersistentStore:store error:nil]; [__persistentStoreCoordinator release]; __persistentStoreCoordinator = nil; [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error]; if (error) { NSLog(@"filemanager error %@", error); } // recreate the stack __managedObjectContext = [self managedObjectContext]; } 

But I get this error when I try to insert objects into the repository afterwards:

 This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation. 

Update: I tried freeing MOC and MOM before deleting persistent storage, but I still get the same error.

+6
source share
3 answers

This is how I make the "reset data" function in several applications:

 - (void)reset { // Release CoreData chain [_managedObjectContext release]; _managedObjectContext = nil; [_managedObjectModel release]; _managedObjectModel = nil; [_persistentStoreCoordinator release]; _persistentStoreCoordinator = nil; // Delete the sqlite file NSError *error = nil; if ([fileManager fileExistsAtPath:_storeURL.path]) [fileManager removeItemAtURL:_storeURL error:&error]; // handle error... } 

Basically, I just release the CoreData chain and then delete the persistentStore file. This is what you are trying to do without using removePersistentStore , which is all the same to me, as I will just rebuild the persistentStore coordinator later. Then, the next time the kernel data is called, the chain is rebuilt transparently using constructors with the same type design:

 - (NSManagedObjectModel *) managedObjectModel { if (!_managedObjectModel) _managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; return _managedObjectModel; } 
+9
source

You can do it from the outside, given that you only need to do this when developing your application. I have a terminal where I delete the storage manually before restarting my application. All you need to know is the place where it is located. I register it for the console every time my application works with the following code:

 [[CoreDataSingleton sharedManager] managedObjectContext]; //be sure to create the store first! //Find targeted mom file in the Resources directory NSString *momPath = [[NSBundle mainBundle] pathForResource:@"Parking" ofType:@"mom"]; NSLog(@"momd path: %@",momPath); 

Hope this helps!

+2
source

Before attempting to delete a repository, you must ensure that the managed entity context attached to the persistent repository. Otherwise, the context will cause this error.

+2
source

Source: https://habr.com/ru/post/886512/


All Articles