Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data lightweight migration issue

Tags:

iphone

My app is currently in the app store, and I am updating it with a single added Attribute to the data model. I added a model version and set it to current.

Everything works but when I test install a new version of the app over an old version containing data, the app fails to load with no error messages. It will continue to fail (just briefly flashing on the screen,) until I either restart the device, or install the updated app again, either through XCode or iTunes, then the app runs fine and the data has migrated properly.

My fear is that if this happens to customers, they will delete the app before reinstalling and lose all their data. Does anyone have any insight? Any help is appreciated

Thanks, I am using the following code in the App Delegate for data migration:

- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DataStore" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
    return managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DataStore.sqlite"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"DataStore" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
     }
     NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], 
     NSMigratePersistentStoresAutomaticallyOption, 
     [NSNumber numberWithBool:YES], 
     NSInferMappingModelAutomaticallyOption, nil];     
     NSError *error;
     persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
         // Update to handle the error appropriately.
         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
         exit(-1);  // Fail
    }         
    return persistentStoreCoordinator;
}
like image 249
3D Monkey Avatar asked Nov 14 '22 05:11

3D Monkey


1 Answers

Are you sure that you made no changes to the previous version of your model? This behaviour sounds like Core Data cannot find a model for the persistent store that you have on the device.

You should be able to see any Core Data errors in the console log when you start up the app that contains an old version of your persistent store.

Also at what point did you add your new attribute? If you added it before you created the new version both the old and new version would have the attribute. Check your old model and make sure that the new attribute is not there.

like image 73
DigitalBytes Avatar answered Dec 28 '22 06:12

DigitalBytes