【发布时间】:2014-03-01 06:48:10
【问题描述】:
我尝试在 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 中完美运行。由于这两个应用程序之间没有同步,每个应用程序都处理自己的数据库。有什么我忘记了吗?
【问题讨论】:
-
你为什么使用 NSPersistentStoreUbiquitousContentNameKey 的 AppID ?大概 iOS 应用 AppID 和 OS X 应用 AppID 不一样,或者这实际上是在真实应用中硬编码的?在任何情况下 NSPersistentStoreUbiquitousContentNameKey 和 iCloud containerID 对于想要同步同一个 Core Data 存储的所有应用程序都必须相同。您在 Xcode 中的通用容器是
sample.sample.sample,在您的代码中是 `com.sample.sample' -
我也有类似的问题。当我尝试在我的 os x 应用程序中与 iCloud 同步时,出现此错误:__60-[PFUbiquitySetupAssistant canReadFromUbiquityRootLocation:]_block_invoke690(1489): CoreData: Ubiquity: Attempting to download Peers hit a Serious error for peers to download Error Domain=BRCloudDocsErrorDomain Code =5 "(BRCloudDocsErrorDomain 5: URL 处没有文档)"
标签: ios macos icloud core-data-migration