MagicalRecord: delete all data and set the underlying data stack again

For each application update, I would like to completely erase the entire Core Data database, and then configure it again. I did not succeed. I tried different things, this seems to be the closest that I came. I found several SO posts, but none of the solutions worked for my purposes.

I use MagicalRecord , which provides several short methods for getting various objects. Here is my code:

 if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"buildVersion"] intValue] < [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] intValue]) { NSPersistentStore *store = [NSPersistentStore defaultPersistentStore]; NSError *error; NSURL *storeURL = [NSPersistentStore defaultLocalStoreUrl]; NSPersistentStoreCoordinator *storeCoordinator = [NSPersistentStoreCoordinator defaultStoreCoordinator]; [storeCoordinator removePersistentStore:store error:&error]; [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error]; [[NSUserDefaults standardUserDefaults] setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"buildVersion"]; } [MagicalRecord setupCoreDataStack]; 
+4
source share
1 answer

There is an NSPersistentStore+MagicalRecord category that allows you to get the url for persistent storage:

 + (NSURL *) MR_urlForStoreName:(NSString *)storeFileName; 

You can use the URL of this call to remove the SQLite persistent SQLite repository using NSFileManager.

+4
source

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


All Articles