2014-02-22 2 views
0

내 CoreData 엔티티 중 하나에 속성을 추가하고 앱을 실행할 때마다 예외가 발생하고 CoreData의 모든 항목 (내가 만든 모든 객체)이 삭제됩니다. 내 AppDelegate에에서엔티티의 속성을 변경할 때 CoreData 정보가 지워지는 이유는 무엇입니까?

:

- (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의 오른쪽에있는 세부 정보 패널에서 새 모델 버전을 선택하고 현재 모델 (녹색 체크가 있어야 함)로 설정했는지 확인하십시오.

이제 새 모델을 변경하고 앱을 실행할 때 기존 모델을 업그레이드해야합니다.

아직 개발 초기 상태이고 기존 코어 데이터 저장소에있는 것을 보관하지 않으려는 경우 앱을 다시 실행하기 전에 삭제하십시오.

관련 문제