Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete data from Core Data after a Lightweight Migration
    primarykey
    data
    text
    <p>In upgrading my application from v1 to v2, I've made a few small changes to the Core Data Model. The changes are just adding new attributes to the models.</p> <p>I have versioned the data model with the before and after changes and implemented the following code in my App Delegate:</p> <pre><code>- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ISDEmployees.sqlite"]; NSLog(@"storeURL:%@",storeURL); NSError *error = nil; // Create a dictionary for automatic lightweight core data migration NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; // Set up the persistent store and migrate if needed if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return __persistentStoreCoordinator; } </code></pre> <p>Basically the standard persistentStoreCoordinator with the addition of the migration options. This code works great and my database is successfully updated. The problem that I'm having is that after the database update, I need to refresh all of the data in the database so that the new columns are populated. I was thinking that I would delete the data from the relevant entities/tables and force the application to re-download a new dataset with the added columns/attributes.</p> <p>I'm not sure how/where to perform the delete/updates. The general application flow is this:</p> <ul> <li>Log in with validation against an web API</li> <li>On successful login, call the API and get latest added/updated records.</li> <li>Display the updated data</li> </ul> <p>I know I can check to see if a migration is needed by adding this code to persistentStoreCoordinator:</p> <pre><code>// Get the current data store meta data BOOL migrationNeeded = NO; NSDictionary *existingStoreData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&amp;error]; if (existingStoreData) { // Check to see if new model is not the same as the existing mode, meaning a migration is required if (![self.managedObjectModel isConfiguration:nil compatibleWithStoreMetadata:existingStoreData]) { migrationNeeded = YES; } } </code></pre> <p>Any help would be greatly appreciated!!</p> <p><strong>Update #1:</strong></p> <p>Based on the feedback below, I've made the following changes:</p> <p>Changed the migrationNeeded from a local to a public class variable on the AppDelegate. On the Login View, I've added the following method:</p> <pre><code>- (void)checkForDatabaseMigration { // Get a copy of the managed object context. If a migration is needed, it will kick it off NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; if ([(AppDelegate *)[[UIApplication sharedApplication] delegate] migrationNeeded]) { // Delete all data from the table } managedObjectContext = nil; } </code></pre> <p>Does that seem right? The code works and the data is removed after migration and a fresh copy is inserted. I just hate to check for migration each time the application starts.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload