2011-12-09 2 views
4

내 응용 프로그램이 핵심 데이터 SQLite 데이터베이스를 사용하고 있습니다. 사용자가 iCloud를 사용하여 장치간에 동기화 할 수있게하고 싶습니다. UIManagedDocument를 사용할 수 있다고 생각했습니다.UIManagedDocument는 파일 패키지 인 문서 만 읽을 수 있습니다.

Apple의 설명서에 따라 서브 클래 싱했으며 새 영구 저장소 파일을 만들어야 할 때 작동합니다. 이것이 내가해야한다는 것을 의미 하는가

을 "UIManagedDocument은 파일 패키지입니다 문서를 읽을 수 있습니다": 내 오래된 영구 저장소 파일을 열 때 사용하려고 할 때 그러나, 나는 다음과 같은 예외 발생 오류 이전 영구 저장소를 UIManagedDocument가 관리하는 새 저장소로 마이그레이션 하시겠습니까? 그렇다면 수동으로해야합니다 (예 : 이전 상점에서 한 번에 하나씩 각 레코드를 읽고 새 상점에 기록하십시오).

미리 감사드립니다.

답변

5

UIManagedDocument는 기본 저장소가 아닌 패키지 (폴더)를 만듭니다. 가게는 아직 거기 있지만 포장에 묻혀있다. 시뮬레이터의 Documents 폴더에 생성 된 파일을 마우스 오른쪽 버튼으로 클릭하면 구조를 볼 수 있습니다. 기본값은 예를 들어 데이터베이스 확장하면 .myappdbw이 될 수 있습니다 프로젝트 설정에서 새 문서 유형을 만들 필요가 .myappdb 경우 귀하의 응용 프로그램 파일 형식에 대한 새로운 확장을 작성하기 만하면 무엇

mydocument.foo 
    -> StoreContent 
     -> persistentStore 

입니다 . .myappdb

에 대한 항목에서 모든 설정을 복사 할 수 있습니다. 그런 다음 기존 매장 관리자에게 전달하는 대신 mydocumenturl에서 기존 문서를 여는 시점에서 위의 디렉토리 구조를 만듭니다.

NSURL *newurl = [[mydocumenturl URLByDeletingPathExtension] URLByAppendingPathExtension:@"myappdbw"]; 
NSURL *desturl = [newurl URLByAppendingPathComponent:@"StoreContent"]; 
[[NSFileManager defaultManager] createDirectoryAtURL:desturl withIntermediateDirectories:YES attributes:nil error:NULL]; 
NSURL *finalurl = [desturl URLByAppendingPathComponent:@"persistentStore"]; 

다음은

[[NSFileManager defaultManager] moveItemAtURL:mydocumenturl toURL:finalurl error:NULL]; 

를 생성 한 폴더 시스템으로 기존의 데이터베이스를 이동 한 다음이 UIManagedDocument

UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:newurl]; 

도움이 될 것입니다 링크를 번들 URL을 전달할 수 있습니다 iCloud 통합의 경우

http://developer.apple.com/library/ios/#releasenotes/DataManagement/RN-iCloudCoreData/_index.html

약속 된 샘플 코드의 대부분이 지금까지는 나타나지 않았지만 다른 한편으로는 그것의 대부분은 간단히 추론하기에는 신비 스럽습니다. WWDC2011 세션 107,116 및 315에서 더 많은 힌트를 찾아보십시오.

그러나

는 기존 문서를 마이그레이션하는이 방법을 사용하려는 경우 DONT 당신이 할 때 패키지가 변경되기 때문에 마이그레이션 지점에서 NSPersistentStoreUbiquitousContentNameKey을 설정할 수 있습니다. 위의 문서는 그것을 아주 잘 설명합니다.

1

이 정보를 제공해 주셔서 감사합니다. 나는 더 단순한 해결책을 찾았다 고 생각한다.

방금 ​​이전 영구 저장소 위치와 다른 파일 이름으로 새 UIManagedDocument을 만듭니다.내 UIManagedDocument 서브 클래스에서

, 나는 configurePersistentStoreCoordinatorForURL 방법을 무시하고 한 번이 마이그레이션을 수행

- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)storeURL ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error 
{ 
    // If legacy store exists, copy it to the new location 
    NSFileManager* fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:legacyPersistentStoreURL.path]) 
    { 
     NSError* thisError = nil; 
     [fileManager copyItemAtURL:legacyPersistentStoreURL toURL:storeURL error:&thisError]; 
     [fileManager removeItemAtURL:legacyPersistentStoreURL error:&thisError]; 
    } 

    return [super configurePersistentStoreCoordinatorForURL:storeURL ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error]; 
} 
+0

더 나은 사용'[파일 관리자 replaceItemAtURL : storeURL withItemAtURL : legacyPersistentStoreURL backupItemName : 무기 호 옵션 : NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL : 전무 오류 : & thisError ];'copy는 기존 파일을 덮어 쓰지 않기 때문에 copy와 remove 대신에 사용됩니다. – Shmidt

관련 문제