I'm making quite a few changes to the object model for our app upgrade release ie. entities added / removed, new attributes and relationships. It seems like the work would really add up for a proper core data migration. Since the data primarily serves as a cache to enhance the offline browsing experience. at this point doesnt really need migration I would think it would be a whole lot simpler if it were just blown away and recreated.
Based on various posts I've come across on this topic the general strategy is to
- detect that the model has changed (by catching the exception during initialization of the managedObjectContext)
- delete the persistent store (in our case on iOS the sqlite file)
- reinitialize an objectModel with the latest schema reinitialize a persistent store with the new model
This is the code that reinitializes the objectModel
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"<model name>" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
and recreating the objectModel and store with
objectManager = [RKObjectManager objectManagerWithBaseURL:
[NSString stringWithFormat:@"http://%@/v3",
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"APIDomain"]]];
NSManagedObjectModel *objectModel = [self managedObjectModel];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:storeName usingSeedDatabaseName:nil managedObjectModel:objectModel delegate:nil];
However, I get the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'UTCity''
I feel like it's pretty close since restarting the app succeeds in creating a new store runs correctly.
-PF