2011-12-31 15 views
26

iCloudをアプリケーションで使用するために、既存のローカルストアをユビキタスストアに移行する必要があります(ユーザーがリクエストした場合)。既存のコアデータストアをiCloudと同期する

アップルのデベロッパーフォーラムや他の場所で、私はこのアプローチを採用しましたが、これは一貫して機能しません。私は実際にそれが動作するのを見ましたが、デバイス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

ほとんどの場合、アプリにはほとんど常に店舗が1つしかありません。 2つのストア(activeStoreIndexなど)の大文字小文字を処理するためのコードは、大文字小文字の区別です。 – ed94133

+1

migrateStoreはどこで定義され、割り当てられていますか? – Jim

答えて

6

iCloud Coreのデータ同期がひどく壊れています。より信頼性の高いサードパーティのソリューションはTICoreDataSyncです。 this forkは、iCloudのコアデータ同期の実装に頼らずに、iCloudでの同期をサポートしています。 iCloudを使用してファイルを同期し、Core Dataオブジェクトを独自に同期して処理するだけです。

私はiCloud Core Dataの同期をあきらめ、代わりにこのライブラリを使ってアプリケーションを構築しています。これまでのところ、私はアップルの実装で見た問題のどれもなく、素晴らしい仕事をしています。

+0

TICDSは積極的に開発されていません。私は同じ原理に基づいた新しいフレームワーク[Ensembles](http://ensembles.io)を開発し、TICDSで学んだ教訓を説明しました。 –

1

あなたはそれを動作させましたか?

がない場合は、ここで尋ねてみ - Weird Errors using CoreData with iCloud

または[はいあれば、私たちに解決策を与える:)

2

を私は今、少なくともために、iCloudの統合をあきらめました。私は、誤ったコアデータのパフォーマンスを考えて、ユーザーにiCloud経由で信頼できる同期ソリューションを提供できるとは思わない。私はiOS 5.1の希望を抱いています。

+4

私が働いていた会社も、コアデータを持つ既存のアプリケーションにiCloudを統合したいと思っていましたが、正しく動作しないと言いました。それは地獄のようだった! 2月のリリース後、クラッシュやデータの消失に関する多くの否定的なフィードバックを得ました。会社は8月に倒産しました:Dちょうどその物語を伝えたい... –

関連する問題