Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iCloud:在IOS和OSX之间同步核心数据_Ios_Macos_Icloud_Core Data Migration - Fatal编程技术网

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

iCloud:在IOS和OSX之间同步核心数据,ios,macos,icloud,core-data-migration,Ios,Macos,Icloud,Core Data Migration,我尝试在IOS和OSX之间同步核心数据。在这两个应用程序中,我的配置相同: 同样的权利: 我还在sqlite文件和url的相同名称中为store coordinator使用相同的代码: NSManagedObjectModel* managedModel = [NSManagedObjectModel mergedModelFromBundles:nil]; NSPersistentStoreCoordinator* storeCooordinator = [[NSPersistentSto

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

同样的权利:

我还在sqlite文件和url的相同名称中为store coordinator使用相同的代码:

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中完美运行。每个应用程序处理自己的数据库,因为这两个应用程序之间没有同步。有什么是我忘记的吗?

您需要非常小心,您在授权中使用的标识符与您在访问ubiquity容器的源代码中使用的标识符相匹配

同步多个应用程序(例如Mac应用程序和iOS应用程序)时的一个复杂问题是,您需要检查它们是否具有相同的团队标识符。如果没有,您将希望为iCloud授权明确填写团队id,而不是使用
TeamIdentifierPrefix
变量。选择一个团队ID并将其用于两个应用程序

在您的特定示例中,看起来您已经为以
sample.sample.sample
结尾的容器id设置了权限。假设每个应用程序的团队id相同,您的容器id应该是
xxxxxxxxx.sample.sample.sample
,其中第一部分是您的团队id。我不知道在这个例子中
app\u标识符是什么,但应该检查它以确保它是
sample.sample.sample
。(我猜不是。)

NSPersistentStoreUbiquitousContentNameKey
设置基本上可以是您喜欢的任何商店标签。它不必使用~甚至是反向DNS形式。例如,您可以将其称为“Store1”

我发现,查看所有设置是否正常的最简单方法是转到Mac上的
~/Library/Mobile Documents
文件夹,并查找应用程序容器。您可以直接在Finder中浏览内容


不幸的是,这可能是使用iCloud获取核心数据最容易的部分。将会有更多的障碍,它们往往更具挑战性。不断有新的解决方案出现,用于同步核心数据,包括框架和,这两者都与iCloud配合使用。(披露:我对这两个项目都有贡献,并创建了Ensembles项目。)

为什么要将AppID用于NSPersistentStoreUbiquiousContentNameKey?大概iOS应用程序AppID和OS X应用程序AppID不一样,或者这实际上是在实际应用程序中硬编码的?在任何情况下,所有希望同步同一核心数据存储的应用程序的NSPersistentStoreUbiquitousContentNameKey和iCloud containerID都必须相同。Xcode中的ubiquity容器是
sample.sample.sample
,而在代码中它的'com.sample.sample'我也有类似的问题。当我尝试在我的os x应用程序中与iCloud同步时,我收到以下错误:uuu 60-[pUbiquitySetupAssistant可以从UbiquityRotLocation:block_invoke690(1489):CoreData:Ubiquity:尝试下载对等方时,对等方下载错误域=brCloudDocErrorDomain代码=5时发生严重错误(brCloudDocErrorDomain 5:URL处无文档)谢谢你的回答。我浏览到osx上的移动文档文件夹,该文件夹已经创建。由于,每次我在osx上运行应用程序时,[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:]都会显示“使用本地存储:1”,并且从不切换到“使用本地存储:0”。在ios上工作正常,而不是在osx上。我的persistant store coordinator中有什么东西吗(请参见上面的代码)?这里是指向示例应用程序的链接-查看它们是否在您的环境中运行,然后检查代码差异。