2012-04-26 9 views
6

私は既存のアプリケーションでコアデータを使用しています。今ではiCloudを統合して、ユーザーがiOSデバイス間でコンテンツを同期できるようにしたいと考えています。すべて新たな付加データレコードがすべて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 Developer's forum(https://devforums.apple.com/thread/126670?tstart=0)にアクセスするには、有効な開発者IDが必要です。スレッドの最後を見てください(かなり長いです)。 –

答えて

1

iCloudでCoreDataを使用してWWDC 2012セッションをルックアップします。彼らは最後のセクションで、播種の話題の下でこれについて議論します。彼らはまた、サンプルを持つサイトから得ることができるいくつかのサンプルコードを持っています。要するに、2つの永続ストアを開き、フェッチサイズを設定し、ソースからバッチを取得し、それらをループして新しいストアに追加し、バッチサイズの間隔で保存してリセットする必要があります。ものすごく単純。

関連する問題