简体   繁体   中英

Core data Versioning (Lightweight migration) Issue

I am using core data in my app and its first version was released in october. Then i made some changes in the data model and uploaded the next version. It started crashing on the device of existing users(crashing on startup), but it was running properly when they re install it or when new user download it. Then i learned about versioning and created a new version ie 1.1 and added following line of code in app delegate

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{
if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"golfCourse.sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

NSDictionary *options = @{
                          NSMigratePersistentStoresAutomaticallyOption : @YES,
                          NSInferMappingModelAutomaticallyOption : @YES
                          };

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

     Typical reasons for an error here include:
     * The persistent store is not accessible;
     * The schema for the persistent store is incompatible with current managed object model.
     Check the error message to determine what the actual problem was.


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

     If you encounter schema incompatibility errors during development, you can reduce their frequency by:
     * Simply deleting the existing store:
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
     @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    abort();
}

return _persistentStoreCoordinator;}

I tested this on simulator and on iphone and it was working properly, but when i uploaded it. It started crashing again on startup for existing app users.

There is no crash log on itunes.

To test this issue i created another version of data model and followed this approach to test model versioning. It worked properly. App is crashing on start up so i guess change in data model is the reason. But i am not sure why is it crashing after adding appropriate options(Lightweight migration).

Do you have a new version model that reflects your changes? Or you have just done your changes in a unique xcdatamodeld file? To create anew model version:

  1. Select your xcdatamodel file
  2. Editor -> Add Model Version
  3. Select again your xcdatamodel file, and in the right panel find Model version and set the current to the model you have just created

Check https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmModelFormat.html#//apple_ref/doc/uid/TP40004399-CH3-SW1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM