ios - iCloud:在 IOS 和 OSX 之间同步核心数据

标签 ios macos icloud core-data-migration

我尝试在 IOS 和 OSX 之间同步核心数据。在这两个应用程序中,我有相同的配置:

enter image description here

以及相同的权利:

enter image description here

我还在 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 配合使用的最简单的部分。将会有更多的障碍,而且往往更具挑战性。不断有新的解决方案用于同步核心数据,包括 TICDS框架和Core Data Ensembles ,两者都可与 iCloud 配合使用。 (披露:我为这两个项目做出了贡献,并创立了 Ensembles 项目。)

关于ios - iCloud:在 IOS 和 OSX 之间同步核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567075/

相关文章:

ios - 如何创建 list 用户体验体验?

macos - 使用 iTerm2 作为 X11 终端(用于字体、主题等)?

objective-c - iCloud:获取文件的现有公共(public) URL

ios - 具有多个部分的 UITableView 中的原生广告

iphone - 一个具有两个目标的 Xcode - 但 Localizable.string 文件的不同选择

python - OSX 上的 Tensorflow 0.10 (CUDA) 在 python 导入时出现段错误

java - 有没有办法找到 OS X 上安装的 Java 版本的完整列表?

iphone - 如何在 iCloud 中搜索上传的文件

ios - NSMetadataQueryDidUpdateNotification 不工作

ios - 音频导出失败 iOS Swift