This is quite simple - once you know where to look.# In your AppDelegate you set-up the NSPersistentStoreCoordinator - and you need to add some options to this to tell it to handle auto-migrate:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle error
NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
}
然后,你需要做:
- Select your xcdatamodeld file
- Select the Editor Menu at the top - then choose Add Model Version
- Now your xcdatamodeld file have two (modelname.xcdatamodel & modelname2.xcdatamodel ) .
- Now modelname.xcdatamodel have the green check mark implies it is current version, but we need to change the modelname2.xcdatamodel as a current version
- Select the xcdatamodeld file and then select the View Menu at the top - then Choose Utilities - then Choose the Show File Inspector is shown in right side of Xcode and then Select the Versioned Core Data Model - have Current(DropDownList) - select modelname2(the one you just made current version have green check mark).
- Now when you install this version onto a device that has the old model - it will automatically upgrade that model to the new model.
保存所有经修改的档案,然后,一旦你准备部署最新消息,你就会删除所有内部档案,并用最老和最新的模式进行部署。 反之亦然(4,5,6)