2015-02-11 224 views
0

我的遷移有一些問題。 遷移之前,我曾在我的模型2個表:核心數據模型遷移步驟

  • 食物:名稱(字符串),類別(字符串),等等...
  • CartFood:名稱(字符串),類別(字符串) etc ...

我需要創建一個新的實體「類別」,並將兩個表的類別屬性轉換爲一對多關係。我也想爲Food實體添加屬性並創建其他實體。

的步驟我有如下如下:

1-添加模型版本,並創建新的類別的實體,刪除類別屬性,建立關係,添加新的屬性,等等

2-用相同的代碼

3-創建我的自定義實體遷移策略類(它們是NSEntityMigrationPolicy的子類)創建映射模型

4-禁用輕量級遷移

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
    // Stop that right now if necessary 
    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    // Store URL 
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"LeSecretDuPoids.sqlite"]]; 

    // Get store 
    NSError *error = nil; 
    NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption : @YES, NSInferMappingModelAutomaticallyOption : @NO }; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    return persistentStoreCoordinator; 
} 

但是,當我推出新的應用程序,我有以下錯誤,沒有日誌顯示在控制檯:

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) 

我錯過了什麼嗎?

Regards, Sébastien。

回答

0

與其排除自定義遷移(這可能很困難),我可能建議您忽略這兩個字符串屬性(或將它們重用於其他內容)並使用輕量級遷移?

添加商店後,您可以將舊的字符串值複製到關係中,並將它們排列在幾行代碼中。例如。

// create all necessary category objects 
// fetch them and all food objects 
for (Food *food in allFoodObjects) { 
    Category *category = [allCategories filteredArrayUsingPredicate: 
    [NSPredicate predicateWithFormat:@"title = %@", food.oldCategory]].firstObject; 
    if (category) { 
    food.category = category; 
    } 
} 
// repeat with cartfood 
+0

是否可以使用NSEntityMigrationPolicy子類輕量級遷移? – Seb 2015-02-13 09:04:26

+0

如果我忽略這兩個字符串,當創建新的商店時,類別關係將爲空,我將無法獲取舊值:/ – Seb 2015-02-13 09:05:35

+0

不,請閱讀我的答案。你必須親自填充新的關係。但它比使用調試自定義遷移更簡單。 – Mundi 2015-02-13 12:56:53