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.
Matthias bauch
source share