2012-07-17 50 views
3

我遵循这个漂亮的教程http://mipostel.com/index.php/home/70-core-data-migration-standard-migration-part-2来做我的核心数据迁移。映射模型为NULL - 核心数据迁移

出于某种奇怪的原因,我总是得到NULL在mappingModel在这些行:

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

(在链接的代码行191)

我试图创建模型的一个非常简单的衍生版本,我重新创建了一次mappingModel 1000次,确保映射模型文件位于项目目录中 - 但此调用始终返回NULL。

有人有什么想法吗?

ps我只是想知道设置迁移选项被称为AFTER使用映射模型。

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(); 
    } 

(线123 ...)

反正

为什么不能映射模型可以找到?

pss忍不住回头:-)这个核心数据迁移的东西与做简单的SQL数据库迁移相比太复杂和困难 - 浪费太多时间。

所以一个大感谢提前!

+0

事实上,因为它证明了教程似乎是错误的 - 所以尝试找到另一个在浪费尽可能多的时间之前,我做了.... :-() – user387184 2012-07-17 09:25:29

回答

4

我遵循同样的教程,并最终不得不通过manualy 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]; 

我发现我的映射模型的文件名看在我的应用程序的包打开我的映射模型。

+0

非常感谢 - 希望这也会帮助其他人以及避免浪费那么多时间。 !顺便说一下,其余的工作是否正确?我发现很奇怪的是,在代码检查是否需要迁移之后设置了迁移选项。这就是为什么我选择了另一条路线的另一个原因...... – user387184 2012-07-17 16:00:16

+0

我最终不得不修改那个教程中的代码,但它让我走上了正确的轨道。我正在撰写关于我的迁移过程的博客文章,我会在完成后发布链接。 – 2012-07-17 20:55:55

+0

为什么在迁移之后设置选项会很奇怪?这些选项仅用于addPersistentStoreWithType,它们与自定义迁移无关。他们甚至可能是零,因为他们将用于默认迁移,因为这里执行的迁移是自定义的。 Apple还指出,检查addPersistentStoreWithType中的迁移代价很高,并建议在之前使用isConfiguration:compatibleWithStoreMetadata,这就是为什么在addPersistentStoreWithType之前进行检查的原因;) – PostCodeism 2012-11-07 12:34:02