2012-04-26 6 views
6

기존 애플리케이션에서 핵심 데이터를 사용하고 있습니다. 이제 사용자가 iOS 기기간에 콘텐츠를 동기화 할 수 있도록 iCloud를 통합하려고합니다. 모든 새로운 추가 데이터 레코드는 모든 iOS- 사이에 자동으로 동기화되는 코드로iOS : 기존 핵심 데이터 데이터베이스를 iCloud로 마이그레이션

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

if (persistentStoreCoordinator_ != nil) { 
    return persistentStoreCoordinator_; 
} 

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"<DB_Name>.sqlite"]; 

persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

NSPersistentStoreCoordinator* psc = persistentStoreCoordinator_; 

if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     // Migrate datamodel 
     NSDictionary *options = nil; 

     // this needs to match the entitlements and provisioning profile 
     NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"<App_Identifier>"]; 
     NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"]; 
     if ([coreDataCloudContent length] != 0) { 
      // iCloud is available 
      cloudURL = [NSURL fileURLWithPath:coreDataCloudContent]; 

      options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
         @"<App_Name>.store", NSPersistentStoreUbiquitousContentNameKey, 
         cloudURL, NSPersistentStoreUbiquitousContentURLKey, 
         nil]; 
     } else { 
      // iCloud is not available 
      options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
         nil]; 
     } 

     NSError *error = nil; 
     [psc lock]; 
     if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
     { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     } 
     [psc unlock]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSLog(@"asynchronously added persistent store!"); 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil]; 
     }); 

    }); 

} else { 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
          nil]; 

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

return persistentStoreCoordinator_; 
} 

: 나는 (물론 자리가 내 코드를 작성하는) 내 NSPersistentStoreCoordinator에 대해 다음 코드를 작성한 것을해야 할 일 기기가 정확히 작동해야합니다.

하지만 내가 원하는 것은 의 모든 기존 데이터 레코드가 모든 장치간에 동기화됩니다. 아직 작동하지 않습니다. 기존 레코드는 앱에서 계속 사용할 수 있으며 사용할 수 있지만 동기화되지는 않습니다.

iCloud와 동기화 된 모든 기존 데이터 레코드를 얻으려면 어떻게해야합니까? 나는 방법으로 약간 실험했다.

migratePersistentStore:toURL:options:withType:error: 

그러나 아무런 성공도하지 못했다.

어떤 도움을 주셔서 감사합니다.

+0

기존 데이터를 마이그레이션하는 데 특별한 코드가 필요합니다. 이미 Google 솔루션이있었습니다. 이 스레드는 Apple 개발자 포럼 (https://devforums.apple.com/thread/126670?tstart=0)에서 확인할 수 있습니다. 액세스하려면 유효한 개발자 ID가 있어야합니다. 스레드의 끝 부분을 먼저 봅니다 (꽤 오래되었습니다). –

답변

1

iCloud와 함께 CoreData를 사용하여 WWDC 2012 세션을 찾으십시오. 그들은 종자의 주제 아래 마지막 절에서 이것을 토의합니다. 또한 예제가있는 사이트에서 얻을 수있는 몇 가지 샘플 코드가 있습니다. 즉, 2 개의 영구 저장소를 열고 패치 크기를 설정 한 다음 원본에서 배치를 가져 와서 루프를 반복하여 새 저장소에 추가하고 배치 크기 간격으로 저장 및 재설정해야합니다. 아주 간단합니다.

관련 문제