开发者

Core Data lightweight migration issue

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;
}


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.


Don't forget that you also need both the source and destination schemas available in your app for the migration to work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜