5

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:[NSNumber 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.

westsider
  • 4,967
  • 5
  • 36
  • 51
the Reverend
  • 12,305
  • 10
  • 66
  • 121

1 Answers1

1

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

MCannon
  • 4,011
  • 2
  • 16
  • 12
  • 1
    Yes thank you... I just found it in Marcus Zarra Core Data book... he wrote a big procedure to migrate from each model to the next without writing extra mapping models. Going to check on NSINfersMappingModel Thanks! – the Reverend Nov 30 '10 at 00:02
  • try and keep data migrations to a minimum (ie major releases) as the overhead very quickly baloons(mapping models from every version to the current one for each change.) also, beware that using automatic migration can work from one version to the next, but your model can change enough that from version 16 to 21 is so different that it can't calculate the difference. glad you got it sorted! – MCannon Nov 30 '10 at 00:23
  • @theReverend, I'm thinking of attempting to try and force a sequential migration strategy as you referenced in your first comment. How's the example in the Marcus Zarra book? – Michael Oct 19 '11 at 20:59