Marcus Zarra의 핵심 데이터 북 사본을 얻고 마이그레이션 (5 장)을 읽어야합니다. 그러나, 그것을 실패하면, 알고있는 좋은 몇 가지 기본 사항이 있습니다. 먼저 업데이트 된 앱에서 이전 모델 (스키마)과 새 모델이 모두 필요합니다. 둘째, 새 모델이 "현재 모델"로 태그 지정되어 있는지 확인해야합니다. 셋째, NSPersistentStoreCoordinator를 기존 모델 (디스크에서로드 됨)에서 새 모델로 자동 매핑하는 방식으로 NSPersistentStoreCoordinator를 만들어야합니다. 새 응용 프로그램에서
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (persistentStoreCoordinator)
return persistentStoreCoordinator;
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyDataStore.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
// Use mapping model
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error])
{
[NSApp presentError:error];
}
return persistentStoreCoordinator;
}
업데이트 당신의 오래된 모델은 정확히 기존 응용 프로그램에서 모델과 동일합니다. 이 경우가 확실하지 않은 경우 확신을 위해 취할 수있는 몇 가지 단계가 있습니다. 제가하는 일은 조금 복잡합니다 - 그러나 그것이 도움이된다고 생각할 때 개요를 드리겠습니다.
그래서 엔티티에 추가하는 필드는 완전히 새로운 엔티티 여야합니까? 그런 다음 이전시 스템에서 새 스키마로 마이 그 레이션 할 때 업그레이드를 확인하고 수행 할 수 있습니까? –
아마 읽었을 지 모르지만를 참조하십시오. 모델을 전혀 변경하지 않으려면 새 버전을 만드십시오. 새 속성을 추가하거나 기존 속성을 /로 변경하는 것은 선택 사항이므로 가벼운 마이그레이션은 자유롭게 수행해야합니다. 나는 당신이 '완전히 새로운 존재 (Entity)'를 의미하는지 확신 할 수 없다. 내게는 –
westsider
가볍습니다. –