2014-02-28 43 views
2

我有一個已經使用核心數據的iOS 7應用程序。我已經使用了全新的iOS集成iCloud的到我的應用程序同步通過使用下面的代碼爲例存儲核心數據項的7方法:如何將現有的核心數據iOS 7應用程序的數據遷移到iCloud中?

https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/README.md

這個偉大的工程,除了所有的原始數據在設備上沒有出現在iCloud商店中。我一直聽說我需要遷移數據 - 但我找不到任何有關如何正確執行此操作的示例。有誰知道如何做到這一點?

我不斷收到指出使用migratePersistentStore:toURL:options:withType:error:,但我不知道怎樣才能用這個...

回答

3

以下是一個帶iCloud控制面板的示例應用程序,用於將商店移至iCloud或從iCloud移動商店。要移動現有商店,您需要使用現有選項打開它,但請確保您爲目標商店使用iOS7選項。看看OSCDStackManager中的示例應用程序代碼,如果您有具體問題,然後張貼它們。 http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

- (bool)moveStoreFileToICloud:(NSURL*)fileURL delete:(bool)shouldDelete backup:(bool)shouldBackup { 
    FLOG(@" called"); 

    // Always make a backup of the local store before migrating to iCloud 
    if (shouldBackup) 
     [self backupLocalStore]; 

    NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; 

    // Open the existing local store using the original options 
    id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:[self localStoreOptions] error:nil]; 

    if (!sourceStore) { 

     FLOG(@" failed to add old store"); 
     return FALSE; 
    } else { 
     FLOG(@" Successfully added store to migrate"); 

     bool moveSuccess = NO; 
     NSError *error; 

     FLOG(@" About to migrate the store..."); 
     // Now migrate the store using the iCloud options 
     id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self icloudStoreURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error]; 

     if (migrationSuccess) { 
      moveSuccess = YES; 
      FLOG(@"store successfully migrated"); 
      [self deregisterForStoreChanges]; 
      _persistentStoreCoordinator = nil; 
      _managedObjectContext = nil; 
      self.storeURL = [self icloudStoreURL]; 
      // Now delete the local file 
      if (shouldDelete) { 
       FLOG(@" deleting local store"); 
       [self deleteLocalStore]; 
      } else { 
       FLOG(@" not deleting local store"); 
      } 
      return TRUE; 
     } 
     else { 
      FLOG(@"Failed to migrate store: %@, %@", error, error.userInfo); 
      return FALSE; 
     } 

    } 
    return FALSE; 
} 
1

你移動你現有的商店到不同的路徑,然後調用與toURL的遷移方法設置到您希望您的商店結束的路徑。

您需要傳遞選項,包括iCloud商店需要設置的無處不在設置。

當遷移完成後,您應該擁有兩個存儲副本:放置在一邊的非iCloud和放置了iCloud選項的新iLOoud。如果你願意,你現在可以刪除舊的商店,只需設置你的核心數據堆棧來使用iCloud商店。

看看example中的一些方法。特別要看看以「遷移」開頭的那些。您應該能夠確定將數據遷移到新的雲存儲所採取的步驟。

核心數據同步很難糾正,特別是當您開始進入數據遷移時。值得看看其他核心數據同步選項,如Wasabi SyncEnsembles。他們自動處理遷移和數據合併。 (披露:我開發合奏)