Core Data - Default Migration ( Manual )

后端 未结 2 591
暗喜
暗喜 2020-12-15 10:52

i\'ve read all possible blogs and SO post on the subject - but still not sure what\'s going on. I\'ve also read this but still no luck - their guide to Default migration kin

相关标签:
2条回答
  • 2020-12-15 11:39

    With the help i got from this question i got to solve your issue.

    I have put together a demo app based on the default master/detail template with core data. When run it first time, make sure you have the "TestData" model selected in "TestData.xcdatamodeld". Add some rows, then move to "TestData 2.xcdatamodel" and the migration will make the proper changes.

    The demo app can be downloaded from here

    0 讨论(0)
  • 2020-12-15 11:43

    If you didn't find answer i can suggest small trick. First, idk why this happens but source model, destination model and mapping model have different values of versionHashes for same entities.

    i programmaticaly corrected them and migration happens.

     NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"cdm"];
     NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:fileURL];
    
     NSArray *newEntityMappings = [NSArray arrayWithArray:mappingModel.entityMappings];
     for (NSEntityMapping *entityMapping in newEntityMappings) {
    
    [entityMapping setSourceEntityVersionHash:[sourceModel.entityVersionHashesByName     valueForKey:entityMapping.sourceEntityName]];
    [entityMapping setDestinationEntityVersionHash:[destinationModel.entityVersionHashesByName valueForKey:entityMapping.destinationEntityName]];
            }
    mappingModel.entityMappings = newEntityMappings;
    
            BOOL ok = [migrationManager migrateStoreFromURL:sourceStoreURL
                                                       type:sourceStoreType
                                                    options:nil
                                           withMappingModel:mappingModel
                                           toDestinationURL:destinationStoreURL
                                            destinationType:destinationStoreType
                                         destinationOptions:nil
                                                      error:&error];
    
    0 讨论(0)
提交回复
热议问题