簡體   English   中英

iCloud:在IOS和OSX之間同步核心數據

[英]iCloud: Sync Core Data between IOS and OSX

我嘗試在IOS和OSX之間同步核心數據。 在這兩個應用程序中,我具有相同的配置:

在此處輸入圖片說明

和相同的權利:

在此處輸入圖片說明

我還為sqlite文件和url使用了相同名稱的商店協調器相同代碼:

NSManagedObjectModel* managedModel = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator* storeCooordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedModel];

//-> start iCloud
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


        NSURL* applicationFilesDirectory = [JHDCoreDataDAO applicationFilesDirectory];
        NSURL* storeURL = [applicationFilesDirectory URLByAppendingPathComponent:DATABASE_NAME];

        if(!storeURL) { NSLog(@"Error reading applicationFilesDirectory for given sqlite resouce"); }

        NSString* containerIdentifier = [NSString stringWithFormat:@"%@.%@",TEAM_IDENTIFIER,APP_IDENTIFIER];
        NSURL* iCloud = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:containerIdentifier];

        NSString *iCloudEnabledAppID = @"TEAMID~com~sample~sample";
        NSError* persistentStoreError;

        if (iCloud) {

            NSLog(@"iCloud is working");
            NSLog(@"iCloudEnabledAppID = %@",iCloudEnabledAppID);
            NSLog(@"iCloud URL: %@",iCloud);

            NSString* cloudPath = [[iCloud path] stringByAppendingPathComponent:@"data"];
            NSURL* transactionsLogUrl = [NSURL fileURLWithPath:cloudPath];

            NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                                     iCloudEnabledAppID, NSPersistentStoreUbiquitousContentNameKey,
                                     transactionsLogUrl, NSPersistentStoreUbiquitousContentURLKey,
                                     [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                 nil];

        [storeCooordinator lock];
        if(![storeCooordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL  options:options error:&persistentStoreError])
        {
            NSLog(@"Fehler: %@, %@", persistentStoreError.localizedDescription, persistentStoreError.userInfo);
            abort();
        }
        [storeCooordinator unlock];

    } else {
    //Handle local psc
    }
});

每個應用程序(IOS版本和OSX版本)仍可以在iCloud中完美運行。 每個應用程序都處理自己的數據庫,因為這兩個應用程序之間沒有同步。 我忘了什么嗎?

您需要非常小心,以確保您在權利中使用的標識符與您在源代碼中使用的標識符匹配以訪問普遍存在的容器。

同步多個應用程序(例如Mac應用程序和iOS應用程序)時,一個復雜的事情是您需要檢查它們是否具有相同的團隊標識。 如果不是,您將要顯式填寫iCloud權利的團隊ID,而不是使用TeamIdentifierPrefix變量。 選擇一個團隊ID,並將其用於兩個應用程序。

在您的特定示例中,您似乎已為以sample.sample.sample結尾的容器ID設置了權利。 假設每個應用程序的團隊ID都相同,那么您的容器ID應該是XXXXXXXXXX.sample.sample.sample ,其中第一部分是您的團隊ID。 我不知道在這種情況下APP_IDENTIFIER是什么,但是應該檢查它以確保它是sample.sample.sample (我的猜測是不是。)

NSPersistentStoreUbiquitousContentNameKey設置基本上可以是您希望為商店使用的任何標簽。 它不必使用〜甚至是反向DNS形式。 例如,您可以將其稱為“ Store1”。

我發現查看所有設置是否簡單的最簡單方法是,轉到Mac上的~/Library/Mobile Documents文件夾,然后查找應用程序容器。 您可以直接在Finder中瀏覽內容。

不幸的是,這可能是使Core Data與iCloud一起使用的最簡單的部分。 會有更多的障礙,而且它們往往更具挑戰性。 不斷有新的解決方案可以同步Core Data,包括TICDS框架和Core Data Ensembles ,它們都可與iCloud一起使用。 (公開:我為這兩個項目都做出了貢獻,並建立了Ensembles項目。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM