I'm using Core data in an iOS project. I have the data model setup for automatic data migration whenever I modify entity properties. But I recently made some changes to some entity relationships and now my app crashes with: "Can't find model for source store"
I realize that resetting the app i.e deleting and re-installing will solve this issue, bit I have a live version already, and my users will lose all all their data!
So now I'm trying manual migration, but the iOS docs are not very helpful. For instance, I have this code which I run after creating a model mapping:
NSURL *destinationStoreURL = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"import.sqlite"]]; NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"db.sqlite"]];
//initialize migration manager
NSMigrationManager *migrationManager = [[NSMigrationManager alloc] initWithSourceModel:[[self persistentStoreCoordinator] managedObjectModel]
destinationModel:[[self persistentStoreCoordinator] managedObjectModel]];
//perform migration
NSError *error = nil;
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:[[self persistentStoreCoordinator] managedObjectModel]
destinationModel:[[self persistentStoreCoordinator] managedObjectModel] error:&error];
if (mappingModel == nil) {
NSLog(@"No Mapping model error %@, %@", error, [error userInfo]);
}
[migrationManager migrateStoreFromURL:sourceStoreURL
type:NSSQLiteStoreType
options:nil
withMappingModel:mappingModel
toDestinationURL:destinationStoreURL
destinationType:NSSQLiteStoreType
destinationOptions:nil
error:&error];
Running this code works and resets the database, but I cant find my old data, and when I save any new data, I get an error that there is no persistent store!
Does anyone have any ideas?