iCloud:在IOS和OSX之间同步核心数据 [英] iCloud: Sync Core Data between IOS and OSX

查看:94
本文介绍了iCloud:在IOS和OSX之间同步核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to sync core data between IOS and OSX. At both apps I have the same configuration:

和相同的权利:

我还为sqlite文件和url使用了相同名称的商店协调员:

I also use the same code for the store coordinator within the same name for sqlite file and 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中完美运行.由于这两个应用程序之间没有同步,因此每个应用程序都处理自己的数据库.有什么我忘了的东西吗?

Each app, the IOS Version and the OSX Version, are still running perfectly within the iCloud. Each app handle its own database, due to there is no sync between this two apps. Is there somethings what I have forgotton?

推荐答案

您需要非常小心,以确保在权利中使用的标识符与在源代码中使用的标识符匹配以访问普遍存在的容器.

You need to be very careful that the identifiers you use in the entitlements match what you use in the source code to access the ubiquity container.

同步多个应用程序(例如Mac应用程序和iOS应用程序)时,一个复杂的问题是您需要检查它们各自具有相同的团队标识.如果不是,您将要显式填写iCloud权利的团队ID,而不是使用TeamIdentifierPrefix变量.选择一个团队ID并将其用于两个应用程序.

One complication when syncing multiple apps (e.g. Mac app and iOS app) is that you will need to check that they each have the same team identifier. If not, you will want to fill in the team id explicitly for iCloud entitlements, rather than using the TeamIdentifierPrefix variable. Pick one of the team ids and use that for both apps.

在您的特定示例中,您似乎具有针对以sample.sample.sample结尾的容器ID的权利设置.假设每个应用程序的团队ID都相同,那么您的容器ID应该为XXXXXXXXXX.sample.sample.sample,其中第一部分是您的团队ID.我不知道在这种情况下APP_IDENTIFIER是什么,但是应该检查它以确保它是sample.sample.sample. (我猜不是.)

In your particular example, it looks like you have entitlements setup for a container id that ends in sample.sample.sample. Assuming the team id is the same for each app, your container id should be XXXXXXXXXX.sample.sample.sample, where the first part is your team id. I don't know what APP_IDENTIFIER is in this instance, but it should be checked to make sure it is sample.sample.sample. (My guess is that it isn't.)

NSPersistentStoreUbiquitousContentNameKey设置基本上可以是您想要为商店使用的任何标签.它不必使用〜甚至是反向DNS形式.例如,您可以将其称为"Store1".

The NSPersistentStoreUbiquitousContentNameKey setting can be basically any label you like for your store. It doesn't have to use the ~ or even be reverse-DNS form. For example, you could call it 'Store1'.

我发现最简单的方法来查看是否已全部设置好,然后转到Mac上的~/Library/Mobile Documents文件夹,然后查找应用程序容器.您可以直接在Finder中浏览内容.

I find the easiest way to see if everything is setup OK is to go to the ~/Library/Mobile Documents folder on your Mac, and look for the app container. You can just browse the contents directly in Finder.

不幸的是,这可能是使Core Data与iCloud一起使用的最简单的部分.会有更多的障碍,而且它们往往更具挑战性.不断有新的解决方案可以同步Core Data,包括 TICDS 框架和核心数据集成,两者均可以与iCloud一起使用. (公开:我为这两个项目都做出了贡献,并建立了Ensembles项目.)

Unfortunately, this is probably the easiest part of getting Core Data working with iCloud. There will be many more hurdles, and they tend to be more challenging. There are new solutions coming up for syncing Core Data all the time, including the TICDS framework and Core Data Ensembles, both of which work with iCloud. (Disclosure: I have contributed to both projects, and founded the Ensembles project.)

这篇关于iCloud:在IOS和OSX之间同步核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆