2012-01-02 41 views
20

我幾乎完成了我的第一個iOS應用程序的1.1版本,其中我對核心數據模型進行了兩項小改動。更具體地說,我已經爲我的一個表添加了兩個屬性。這是我的第一個核心數據應用程序,因此我的第一個核心數據更新爲實時應用程序,對於運行代碼可能無法用於當前用戶的問題,我感到有些不安。由於這是一個小小的更新,我猜Apples自動遷移方法可以,但我不想猜測。使用更新的核心數據模型更新iOS應用程序

相關的代碼如下所示:

NSError *error = nil; 
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
{ 

    //NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
}  

在1.1我已經添加NSDictionary *options

在我的視圖控制器我確保所需的行不無這樣的:

if (currentPage.inputType == @"drawing") { 
    [pageView changeToDrawing]; 
} else if (currentPage.inputType == @"text") { 
    [pageView changeToText]; 
} else { 
    currentPage.inputType = @"text"; 
    [pageView changeToDrawing]; 
} 

基本上是這些變化足以或做我必須做點別的?

回答

24

Here is a very helpful article關於核心數據版本化,可以回答你的問題。

總而言之,您需要爲您的項目添加新的模型版本。這對於遷移過程是必需的。

編輯

以上鍊接已經改變,將您重定向到以下鏈接:Core Data Model Versioning and Data Migration

如果一切都失敗了,谷歌:Core Data Model Versioning and Data Migration apple developer

+0

感謝您的回答,我有一個相關的問題。我在創建新模型版本之前添加了屬性,是否應該從我的1.0版本中刪除新屬性,並且只在我的2.0數據模型版本中包含它們,或者沒有關係?謝謝! – Anders 2012-01-02 17:48:56

+2

絕對應該從1.0中刪除它們並添加到下一個版本。其實我最近一直在這個位。 1.0應該與當前部署的*完全相同,否則你將會得到一個異常。這包括屬性和關係。 – Jeremy 2012-01-02 18:01:04

+0

啊,謝謝。得到它的工作。按照你的建議。 :) – Anders 2012-01-02 18:03:10

相關問題