2011-12-31 4 views
26

사용자가 요청한 경우 기존 로컬 스토어를 유비쿼터스 스토어로 마이그레이션해야하는 내 앱으로 iCloud를 사용하려고합니다.기존 코어 데이터 스토어와 iCloud 동기화하기

애플의 dev 포럼과 다른 곳을 둘러 본 후, 나는이 접근 방식을 채택했다. 일관되게 작동하지 않는다. 실제로 작동하는 것을 보았지만 장치 B (iCloud에서 채워짐)에서 여러 번 충돌 한 후에 만 ​​발생합니다.

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

    NSURL *legacyStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:NO]]]; 
    NSURL *upgradedStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:YES]]]; 

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

    if ((IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) && (self.iCloudEnabled)) { 
    NSPersistentStoreCoordinator* psc = persistentStoreCoordinator; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSDictionary *cloudOptions = nil; 
    NSDictionary *localOptions = [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
            [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
            nil]; 


    NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"<CONTAINER ID>"]; 
    NSString *coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:[NSString stringWithFormat:@"logs%d",[self activeStoreIndex]]]; 
    if ([coreDataCloudContent length] != 0) { 
     // iCloud is available 
     cloudURL = [NSURL fileURLWithPath:coreDataCloudContent]; 

     cloudOptions = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
         @"MyAppStore", NSPersistentStoreUbiquitousContentNameKey, 
         cloudURL, NSPersistentStoreUbiquitousContentURLKey, 
         nil]; 
    } else { 
     // iCloud is not available 
    } 

    NSError *error = nil; 
    [psc lock]; 
    if(migrateStore) { 
     migrateStore = NO; 

     NSPersistentStore *srcPS = [psc addPersistentStoreWithType:NSSQLiteStoreType 
      configuration:nil 
      URL:legacyStoreUrl 
      options:localOptions 
      error:&error]; 
     if (![psc migratePersistentStore:srcPS 
      toURL:upgradedStoreUrl 
      options:cloudOptions 
      withType:NSSQLiteStoreType 
      error:&error]) { 
      NSLog(@"Error migrating data: %@, %@/%@/%@", error, [error userInfo], legacyStoreUrl, upgradedStoreUrl); 
      abort(); 
     } 
    } 
    else { 
     if (![psc addPersistentStoreWithType:NSSQLiteStoreType 
      configuration:nil 
      URL:upgradedStoreUrl 
      options:(cloudOptions ? cloudOptions : localOptions) 
      error:&error]) { 
       NSLog(@"Unresolved iCloud error %@, %@", error, [error userInfo]); 
       abort(); 
     } 
    } 
    [psc unlock]; 

    [[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:legacyStoreUrl options:options error:&error]) { 
     // error 
     abort(); 
    }  
    } 

    return persistentStoreCoordinator; 
} 
+0

앱에는 거의 항상 상점이 하나뿐입니다. 2 store (activeStoreIndex 등)의 경우를 처리하기위한 코드는 엣지 경우입니다. – ed94133

+1

migrateStore가 정의되고 할당되는 위치는 어디입니까? – Jim

답변

6

iCloud 핵심 데이터 동기화가 심각하게 손상되었습니다. 보다 안정적인 타사 솔루션은 TICoreDataSync입니다. this fork은 iCloud의 핵심 데이터 동기화 구현에 의존하지 않고 iCloud를 통한 동기화를 지원합니다. iCloud를 사용하여 파일을 동기화하고 Core Data 객체를 자체적으로 동기화하는 처리 만합니다.

iCloud Core Data 동기화를 포기하고 대신이 라이브러리로 내 앱을 제작했습니다. 지금까지는 애플의 구현에서 보았던 어떤 문제도없이 훌륭하게 작동했습니다.

+0

TICDS는 적극적으로 개발되지 않았습니다. 필자는 동일한 원칙을 토대로 한 새로운 프레임 워크 인 Ensemble (http://ensembles.io)을 개발했으며 TICDS에서 배운 교훈을 설명했다. –

2

지금 이상을 위해, 우리에게 솔루션을 제공 -

하지 않을 경우 여기 물어보십시오. 불규칙한 핵심 데이터 성능으로 인해 사용자에게 iCloud를 통해 안정적인 동기화 솔루션을 제공 할 수 있다고 생각하지 않습니다. iOS 5.1에 대한 희망을 갖고 있습니다.

+4

내가 일한 회사는 코어 데이터가있는 기존 앱에 iCloud 통합을 원했지만 제대로 작동하지 않는다고 말했지만 그것은 지옥과 같았다! !!! 2 월에 출시 된 이후 우리는 충돌 및 데이터 손실과 관련하여 많은 부정적인 피드백을 받았습니다. 회사는 8 월에 지불 불능 상태가되었습니다 : D 그냥 이야기를하고 싶었습니다 ... –

관련 문제