4

I follow this nice tutorial http://mipostel.com/index.php/home/70-core-data-migration-standard-migration-part-2 to do my core data migration.

For some strange reason i always get NULL in the mappingModel in these lines:

 NSMappingModel *mappingModel = [NSMappingModel mappingModelFromBundles:nil
                                                                forSourceModel:sourceModel
                                                              destinationModel:destinationModel];

(line 191 in the linked code)

I tried to create a very simple derived version of the model, I recreated a mappingModel a 1000 times, made sure that the mapping model file is in the project directory - but this call always returns NULL.

Anybody has an idea what is wrong here?

ps I was just wondering that setting the migration options is called AFTER the mapping Model is used.

   NSURL *storeUrl = [NSURL fileURLWithPath:storePath];   
    NSError *error;   
    NSDictionary *pscOptions = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption,
                                nil];

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                  configuration:nil
                                                            URL:storeUrl
                                                        options:pscOptions
                                                          error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }   

(lines 123...)

Anyway

Why can't the mapping model be found ?

pss couldn't help holding back :-) this core data migration stuff is much too complicated and difficult compared to doing simple SQL DB migration - wasting soooo much time.

So a BIG THANKS in advance!

user387184
  • 10,953
  • 12
  • 77
  • 147
  • actually as it turned out the tutorial seems to be faulty - so try to find another one before wasting as much time as I did.... :-() – user387184 Jul 17 '12 at 09:25

1 Answers1

4

I followed that same tutorial and ended up having to manualy open my mapping model by URL

  NSString *mappingModelPath = [[NSBundle mainBundle] pathForResource:@"mappingModel10" ofType:@"cdm"];
    NSLog(@"mapping model path:%@", mappingModelPath);
    NSURL *mappingModelUrl = [NSURL fileURLWithPath:mappingModelPath];
    NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:mappingModelUrl];

I found the file name for my mapping model by looking in my App's bundle.

Jesse Crocker
  • 863
  • 5
  • 14
  • thanks very much - hopefully this will help others as well to avoid wasting so much time. ! By the way, did the rest work correctly? I found it really strange that the options for migration were set AFTER the code check whether migration is required. That's another reason why I ended up choosing another route... – user387184 Jul 17 '12 at 16:00
  • I ended having to modify the code from that tutorial quite a bit, but it got me on the right track. Im working on a blog post about my migration process, i'll post a link here when it's done. – Jesse Crocker Jul 17 '12 at 20:55
  • Why would it be weird having the options set AFTER the migration? The options are ONLY used in addPersistentStoreWithType, they're nothing to do with a custom migration. They could even be nil because they would be used for the default migration, where as the migration performed here is custom. Apple also states that checking for the migration in addPersistentStoreWithType is expensive, and advise to use isConfiguration:compatibleWithStoreMetadata before, so that's why makes sense to check before addPersistentStoreWithType ;) – PostCodeism Nov 07 '12 at 12:34
  • 1
    @JesseCrocker Why `ofType:@"cdm"` and not `ofType:@"xcmappingmodel"? – Colas Nov 20 '14 at 10:25
  • 1
    @Colas .xcmappingmodel files are compiled into .cdm files, which are binary plist files. – Jesse Crocker Nov 20 '14 at 12:52
  • @JesseCrocker Thanks ! A bug is O.34 version of cocoapods... The xcmappingmodel were not compiled. Fixed with 0.35. – Colas Nov 20 '14 at 14:39