Migrating through several versions in core data
I have Core Data application and I have been migrating (upgrading) the core data model. Each time I create a new version I create a mapping model for each version. Right now I have 16 versions and I have mapping models that go like this: 1to2.xcmappingmodel 2to3.xcmappingmodel 3to4.xcmappingmodel ...etc. up to 16
This works fine, but a problem arises when one user has a data file with version 10 and updates the application that has version 16. Some how I thought Core Data will automatically upgrade from 10 to 16, but an error shows up that says "Missing Mapping Model". To make sure the mapping models where correct, I upgrade it to each version one by one (10 to 11, 11 to 12, etc..) and it did work... Here is my code.
I specify the model version with this code:
NSBundle *modelWrapper = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"TaskApp_DataModel" ofType:@"momd"]];
NSString *modelPath = [modelWrapper pathForResource:@"TaskApp_DataModel 16" ofType:@"mom"];
NSLog(@"%@",modelPath);
managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
And I define the Migrate Automatic option here:
NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"storedata-sql"]];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNum开发者_运维知识库ber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:dict
error:&error]){
Does anyone know how to upgrade this? Thank you.
it will only try to go from the users existing version(possibly v1), to the current version. if you have 3 versions then you need maps for v1-v2,v2-v3,v1-v3. have all of the 16 versions shipped? if so you may need to start making new migration maps, it may also be worth enabling automatic migration if you haven't tried it before, as it can be very good at filling in the gaps. i think it is:
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSInfersMappingModel];
but you will have to double check that.
sorry to be the bringer of bad news
精彩评论