Reputation: 9857
I have an already shipped CoreData app, I got 9 model outdated versions before coming to the release one (version 10). I would like to remove the long list of development models that lead me to the definitive model.
During development I found this can be easily done by deleting reference in myproject.xcodeproj and removing also version_x.xcdatamodel file in mydata.xcdatamodeld.
However I am trying to investigate of any possible downside, in particular considering that my app is already on the app store. In consideration to the fact that there's no way I would migrate or restore datamodel from earlier version.
On the opposite I just added a new model based on version 10, called version 11 on which I am doing development. I don't know the mechanism of migration, but why do I need model prior to version 10 ?
Upvotes: 2
Views: 191
Reputation: 96
The primary reason for making multiple versions of your data model in the first place is to handle migrations (upgrading the database) more elegantly than just deleting the existing database and creating a new one with the changes.
So if you have previous shipped versions of your app that use the previous data models AND you want the capability of upgrading the database elegantly, then just leave the previous models intact.
If the only reason you created multiple versions of your data model is to keep your data intact during initial development and no one else out there has your app with the previous data models, then delete away. It doesn't matter.
To migrate your data model automatically and elegantly use the following code in your app delegate:
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"]; //change to the name of your database
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]; --delete old model if necessary
//abort(); --Abort App if necessary
}
return _persistentStoreCoordinator;
}
Upvotes: 1