2014-02-22 100 views
0

我每次屬性添加到我的CoreData實體之一,並運行應用程序,我得到了CoreData一個例外,一切(我創建的所有對象)將被刪除。爲什麼在更改實體的屬性時我的CoreData信息被刪除?

在我的AppDelegate:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator == nil) { 
     NSURL *storeURL = [NSURL fileURLWithPath:[self dataStorePath]] 
     _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; 
     NSError *error; 

     if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error]) 
     { 
      [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]; 
      NSLog(@"Deleted old database %@, %@", error, [error userInfo]); 
      [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES} error:&error]; 
      abort(); 
     } 
    } 
    return _persistentStoreCoordinator; 
} 

- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (_managedObjectContext == nil) { 
     NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator; 
     if (coordinator != nil) { 
      _managedObjectContext = [[NSManagedObjectContext alloc] init]; 
      [_managedObjectContext setPersistentStoreCoordinator:coordinator]; 
     } 
    } 
    return _managedObjectContext; 
} 

的錯誤,我得到:

2014-02-23 01:37:08.702 SoundQuiz[8063:70b] Deleted old database Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error 134130.)" UserInfo=0xc8796f0 {URL=file:///Users/Eli/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/7041F45C-D3B2-4B9D-94A0-F5B68482D065/Documents/DataStore.sqlite, metadata={ 
    NSPersistenceFrameworkVersion = 479; 
    NSStoreModelVersionHashes =  { 
     CategoryLevel = <f9824409 52866ec4 6a02251e d7b32d21 430cedd7 2b83ce04 1997e50c 2e0137d6>; 
     GameCategory = <d65d9779 80986a4b da767cae b208d733 7944cebf c146e242 e7c9f0ff fc06eb31>; 
     LevelSound = <2b23da4d ef82c20d 68a5de45 efb7b2f0 26621867 68904b04 04144acb 44fd43fd>; 
    }; 
    NSStoreModelVersionHashesVersion = 3; 
    NSStoreModelVersionIdentifiers =  (
     "" 
    ); 
    NSStoreType = SQLite; 
    NSStoreUUID = "A28E8265-2D65-439E-9634-55482C4ED9F0"; 
    "_NSAutoVacuumLevel" = 2; 
}, reason=Can't find model for source store}, { 
    URL = "file:///Users/Eli/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/7041F45C-D3B2-4B9D-94A0-F5B68482D065/Documents/DataStore.sqlite"; 
    metadata =  { 
     NSPersistenceFrameworkVersion = 479; 
     NSStoreModelVersionHashes =   { 
      CategoryLevel = <f9824409 52866ec4 6a02251e d7b32d21 430cedd7 2b83ce04 1997e50c 2e0137d6>; 
      GameCategory = <d65d9779 80986a4b da767cae b208d733 7944cebf c146e242 e7c9f0ff fc06eb31>; 
      LevelSound = <2b23da4d ef82c20d 68a5de45 efb7b2f0 26621867 68904b04 04144acb 44fd43fd>; 
     }; 
     NSStoreModelVersionHashesVersion = 3; 
     NSStoreModelVersionIdentifiers =   (
      "" 
     ); 
     NSStoreType = SQLite; 
     NSStoreUUID = "A28E8265-2D65-439E-9634-55482C4ED9F0"; 
     "_NSAutoVacuumLevel" = 2; 
    }; 
    reason = "Can't find model for source store"; 
} 

我似乎無法找到我的問題。修改實體時是否總是需要創建新的模型版本?我認爲它會自動遷移?

回答

2

如果創建一個核心數據存儲,然後編輯該核心數據模型,而無需首先創建一個新的模型版本,並且未啓用自動遷移(型號升級),並再次運行應用程序,你會得到上述錯誤和現有商店將不會被打開。

所以首先你需要設置下面

@{NSMigratePersistentStoresAutomaticallyOption:@YES, 
      NSInferMappingModelAutomaticallyOption:@YES} 

的persistentStoreCoordinator選項,然後你需要選擇在Xcode中現有的模型,然後選擇添加模型版本創建一個新的模型版本Xcode中的編輯器菜單。現在請確保您選擇了新的型號版本,並將其設置爲Xcode右側的詳細信息面板中的當前型號(應該有綠色勾號)。

現在你修改的新模式,並在運行應用程序應該升級現有的模型。

或者,如果你還處於發展初期並不在乎保持什麼在現有的核心數據存儲再次運行該應用程序之前,簡單地將其刪除。

相關問題