Reputation: 2897
Every time I add an attribute to one of my CoreData entities and run the app, I get an exception and everything in CoreData (all objects I created) is deleted.
In my AppDelegate:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator == nil) {
NSURL *storeURL = [NSURL fileURLWithPath:[self dataStorePath]]
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError *error;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error])
{
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
NSLog(@"Deleted old database %@, %@", error, [error userInfo]);
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES} error:&error];
abort();
}
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext == nil) {
NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
}
return _managedObjectContext;
}
The error I get:
2014-02-23 01:37:08.702 SoundQuiz[8063:70b] Deleted old database Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error 134130.)" UserInfo=0xc8796f0 {URL=file:///Users/Eli/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/7041F45C-D3B2-4B9D-94A0-F5B68482D065/Documents/DataStore.sqlite, metadata={
NSPersistenceFrameworkVersion = 479;
NSStoreModelVersionHashes = {
CategoryLevel = <f9824409 52866ec4 6a02251e d7b32d21 430cedd7 2b83ce04 1997e50c 2e0137d6>;
GameCategory = <d65d9779 80986a4b da767cae b208d733 7944cebf c146e242 e7c9f0ff fc06eb31>;
LevelSound = <2b23da4d ef82c20d 68a5de45 efb7b2f0 26621867 68904b04 04144acb 44fd43fd>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers = (
""
);
NSStoreType = SQLite;
NSStoreUUID = "A28E8265-2D65-439E-9634-55482C4ED9F0";
"_NSAutoVacuumLevel" = 2;
}, reason=Can't find model for source store}, {
URL = "file:///Users/Eli/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/7041F45C-D3B2-4B9D-94A0-F5B68482D065/Documents/DataStore.sqlite";
metadata = {
NSPersistenceFrameworkVersion = 479;
NSStoreModelVersionHashes = {
CategoryLevel = <f9824409 52866ec4 6a02251e d7b32d21 430cedd7 2b83ce04 1997e50c 2e0137d6>;
GameCategory = <d65d9779 80986a4b da767cae b208d733 7944cebf c146e242 e7c9f0ff fc06eb31>;
LevelSound = <2b23da4d ef82c20d 68a5de45 efb7b2f0 26621867 68904b04 04144acb 44fd43fd>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers = (
""
);
NSStoreType = SQLite;
NSStoreUUID = "A28E8265-2D65-439E-9634-55482C4ED9F0";
"_NSAutoVacuumLevel" = 2;
};
reason = "Can't find model for source store";
}
I can't seem to find my problem. Do I always have to create a new model version when modifying an entity? I thought it would migrate automatically?
Upvotes: 0
Views: 115
Reputation: 9018
If you create a Core Data store and then edit the Core Data model without having first created a new model version and without having enabled automatic migration (model upgrade) and run the application again you will get the error above and the existing store will not be opened.
So first you need to set the persistentStoreCoordinator options below
@{NSMigratePersistentStoresAutomaticallyOption:@YES,
NSInferMappingModelAutomaticallyOption:@YES}
And then you need to create a new model version by selecting the existing model in Xcode and then selecting the Add Model Version from the Editor menu in Xcode. Now make sure you select the new model version and set this as the current model (should have a green tick on it) in the details panel on the right hand side in Xcode.
Now you make changes to the new model and when you run the app it should upgrade the existing model.
Alternately if you are still early in development and don't care to keep whats in the existing Core Data store simply delete it before running the app again.
Upvotes: 2