2012-07-17 51 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