2012-09-05 2 views
2

코어 데이터를 사용하는 모든 사람은 "저장소를 여는 데 사용 된 모델이 저장소를 만드는 데 사용 된 모델과 호환되지 않습니다"라는 메시지를 알고 있습니다.앱 업데이트가있는 핵심 데이터 업데이트 모델?

그럼 시뮬레이터에서 내 앱을 삭제하고 다시 빌드해야합니다.

제 질문은 앱 v 1.0을 제출 한 다음 v 1.1에서 코어 데이터에 일부 엔티티를 추가하는 것입니다. 이는 1.1로 업데이트 한 1.0의 사용자가 데이터를 지우는 것을 의미합니까?

답변

1

모델에 대한 새 모델 버전을 작성하고 데이터베이스를 마이그레이션해야합니다. 모델 변경이 필요한 변경 사항 내에있는 경우 간단한 마이그레이션을 수행 할 수 있습니다. 그렇지 않은 경우 핵심 데이터에 데이터 마이그레이션 방법을 알려줘야합니다. 이전 문서 확인 : http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/Introduction.html

1

기존 데이터 모델의 간단한 확장 기능처럼 들릴 수도 있습니다. 새로운 엔티티를 추가하거나 새로운 클래스를 추가하는 경우, leightweight 마이그레이션이 올바른 방법입니다.

실제로이 경우에는 할 일이 거의 없지만 원래 모델에 두 번째 모델을 추가하십시오. 그것은 중요합니다, 당신은 두 모델을 가지고 다음 응용 프로그램은 새로운 버전뿐만 아니라 아무런 문제없이 첫 번째 버전을로드합니다.

새 모델을 새 모델로 표시하는 것을 잊지 마십시오!

모델 삭제가 정말 번거롭기 때문에 새 모델을 만들 때주의하십시오.

귀하의 코드는 다음과 매우 유사합니다

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


- (NSPersistentStoreCoordinator *) persistentStoreCoordinator { 
    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 

    // Allow inferred migration from the original version of the application. 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DBName.sqlite"]]; 

    NSError *error = nil; 

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl 
                 options:options error:&error]){ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 


    } 
    return persistentStoreCoordinator; 
} 

- (NSManagedObjectModel *) managedObjectModel { 
    if (managedObjectModel != nil) { 
     return managedObjectModel; 
    } 
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; 
    return managedObjectModel; 
} 
관련 문제