Why does CoreData crash when adding an attribute?

Every time I add a new attribute to my CodeData object model, I need to clear the database file, otherwise I get the following error:

2010-11-13 15: 26: 44.580 MyApp [67066: 207] * Application termination due to the uncaught exception 'NSInternalInconsistencyException', reason: '+ entityForName: could not find NSManagedObjectModel for entity name' myApp ''

There should be a way to add additional fields without losing the entire database.

What do I need to do to save my data?

+7
iphone core-data attributes object-model
source share
1 answer

there is a method, and this method is called automatic facilitated migration. To change the object model, you need a code and an additional step.

For code, you need to add two parameters to the method in which you initialize your permanent repository coordinator. something like that:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator_ != nil) { return persistentStoreCoordinator_; } NSString *storePath = [AppDelegate_Shared coredataDatabasePath]; NSURL *storeURL = [NSURL fileURLWithPath:storePath]; // important part starts here 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]) { // and ends here LogError(@"Unresolved error %@, %@", error, [error userInfo]); // Do something } return persistentStoreCoordinator_; } 

Now, if you want to change your model, you need to create a version of the model before you make any changes.
Select your data model and go to the main menu Design -> Data Model -> Add Model Version . Your "old" model will be renamed, and you will make changes to the current model, one that has a green label.
All old models are stored and will be placed in your application, so your application can perform "automatic easy migration" and update the existing database to the new model.

+14
source share

All Articles