NSMigrationManager를 사용하여 데이터를 핵심 데이터 영구 저장소에 추가

StackOverflow https://stackoverflow.com/questions/1630059

문제

하나의 SQLITE 파일 (모델 A와 함께 코어 데이터를 사용하여 작성된)의 내용을 내 응용 프로그램에서 사용하는 다른 SQLITE 저장소 (동일한 모델 A를 사용하는)에 추가하고 싶습니다. 아이디어는 많은 양의 데이터를 빠르게 가져 오는 것입니다.

내가 직면 한 문제는 아래 코드가 한 번만 작동한다는 것입니다. 동일한 코드를 실행하려고하면 두 번의 응용 프로그램이 내 댓글을 표시 한 줄에서 충돌합니다. 모든 도움은 대단히 감사하겠습니다.

NSError **err;
NSURL *importURL = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath]  stringByAppendingPathComponent: @"import.sqlite"]];
NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"applicationdata.sqlite"]];
NSMigrationManager *migrator = [[NSMigrationManager alloc] initWithSourceModel:[self managedObjectModel] destinationModel:[self managedObjectModel]];
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:[self managedObjectModel] destinationModel:[self managedObjectModel] error:err];
NSError **err2;

// the following line crashes when the whole block is ran twice
[migrator migrateStoreFromURL:importURL 
                         type:NSSQLiteStoreType 
                      options:nil 
             withMappingModel:mappingModel 
             toDestinationURL:storeURL 
              destinationType:NSSQLiteStoreType 
           destinationOptions:nil 
                        error:err2];

NSLog(@"import finished");
[migrator release];
도움이 되었습니까?

해결책

해당 코드에서 한 가지 실수가 바로 봅니다.이 방법은 해당 메소드 호출의 오류 인수와 관련이 있습니다. 그만큼 NSError** 방법에 주소를주고 싶다는 것을 의미합니다. NSError*, 오류 객체에 대한 참조를 기록하는 데 사용됩니다 (오류가 발생하는 경우). 지금은 당시에는 스택에 어떤 일이 발생하는지에 따라 유효한 것을 가리키거나 총 쓰레기를 가리킬 수있는 초기화되지 않은 포인터를 통과합니다. 이주자의 방법은이 시점에 쓸 것입니다. 때로는 명백한 효과가 없지만 때로는보고있는 것처럼 충돌이 발생합니다. 그 코드는 다음과 같습니다.

NSError *err2 = nil; //you want to initialize to nil, in case method doesn't modify your pointer at all (i.e. no error occurs)

//the method also returns a BOOL indicating success/failure
if (![migrator migrateStoreFromURL:importURL 
                     type:NSSQLiteStoreType 
                  options:nil 
         withMappingModel:mappingModel 
         toDestinationURL:storeURL 
          destinationType:NSSQLiteStoreType 
       destinationOptions:nil 
                    error:&err2])
{
    //handle the error
}

다른 팁

이것을 지적 해 주신 Brian에게 감사드립니다.

결국 스택의 영구 상점에 추가 SQLITE 파일을 추가하여 매우 잘 작동하며 나중에 FetchRequest를 개별 상점으로 제한 할 수 있습니다.

NSError *error = nil;
NSURL *url = SOME_FILEPATH_URL;
NSPersistentStore *newStore = [persistentStoreCoordinator 
                                    addPersistentStoreWithType:NSSQLiteStoreType
                                                 configuration:nil
                                                           URL:url 
                                                       options:opt
                                                         error:&error];

그리고 우리는 나중에 참조 할 수 있도록 모든 PeristentStores의 사전을 유지합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top