2013-05-09 5 views
1

그 후 약간의 처리를 통해 간단한 마이그레이션을 보관하려고합니다. 이미 경량 프로세스를 수행 했으므로 현재 엔티티를 처리하는 데 도움이 필요합니다.CoreData Lightweight Migration + Custom

이전 모델에서 나는 "Car"라는 엔티티를 사용했고 이제 Person has Cars와의 관계가있는 "Person"엔티티를 추가했습니다.

가벼운 이동 후에 기본 사용자 "John"을 추가하고 모든 자동차를 추가해야합니다.

누구에게 아이디어가 있습니까?

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"]; 

    NSError *error = nil; 
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

    NSDictionary *options = @{ 
           NSMigratePersistentStoresAutomaticallyOption : @YES, 
           NSInferMappingModelAutomaticallyOption : @YES 
           }; 


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

    return _persistentStoreCoordinator; 
} 

답변

2

간단한 마이그레이션을 수행 할 수 있는지 파악할 수 있습니다. 자세한 내용은 this 답변을 참조하십시오. 여기서 깃발을 설정할 수 있으며,이를 기반으로 정상적인 시작 후에 원하는 엔티티를 삽입하는 메소드를 실행할 수 있습니다.

가벼운 마이그레이션은 모든 기존 엔티티를 새 저장소 버전으로 마이그레이션해야하므로 일반적으로 은이 훅을 사용하는 이유가 논리적으로 이유가 없음입니다. 대신, "John"과 그의 자동차가 포함되어 있다면 저장소를 쿼리하고 그렇지 않다면 삽입합니다.

+0

도움 주셔서 감사합니다. 미래에 내 모델의 또 다른 버전을 만들면 어떨까요? 그것을 추적 할 방법이 있습니까? 만약 v2로 업그레이드 할 예정이라면, v3에 업그레이드하면 Vincent를 삽입할까요? –

+0

네, 다른 답변에 설명 된대로 버전에 대해 항상'NSObjectModel'을 쿼리 할 수 ​​있습니다. – Mundi

0

데이터베이스를 한 번 업그레이드하는 경우 경량 마이그레이션 대신 custom migration policy class을 사용하는 것이 좋습니다. 다음은 그 과정을 사용자 정의에 좋은 튜토리얼입니다 : http://9elements.com/io/index.php/customizing-core-data-migrations/

당신이 다음 모델의 또 다른 한 쌍 사이의 사용자 정의 이주 모델 중 하나 쌍의 추정 마이그레이션을 혼합 할 경우, 나는 description of the Core Data methods involved 및 샘플 iterative migration class를 작성했습니다.