Core Data lightweight migration issue
<p>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.</p>

<p>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.</p>

<p>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</p>

<p>Thanks,
I am using the following code in the App Delegate for data migration:</p>

<pre><code>- (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:&amp;error]) {
         // Update to handle the error appropriately.
         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
         exit(-1);  // Fail
    }         
    return persistentStoreCoordinator;
}
</code></pre>
