Cocoa OS X和iOS iCloud Coredata同步

时间:2015-07-29 13:11:58

标签: ios macos cocoa core-data icloud

我有一个适用于iOS和OSX的应用程序,它使用iCloud同步Coredata。在iOS中,我认为它运作良好。问题来自OSX版本。两个版本中使用的代码几乎相同。

我在AppDelegate中有这个:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myStore.sqlite"];    
    NSURL *cloudRootURL=[fileManager URLForUbiquityContainerIdentifier:nil];

    NSString *pathToCloudFile = [[cloudRootURL path]stringByAppendingPathComponent:@"Documents"];
    pathToCloudFile = [pathToCloudFile stringByAppendingPathComponent:@"myAppCloudLogs1"];

    NSURL *cloudURL = [NSURL fileURLWithPath:pathToCloudFile];

    NSString *cloudStoreTitle = @"myStoreCloud";
    NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                      NSInferMappingModelAutomaticallyOption : @YES,
                      NSPersistentStoreUbiquitousContentURLKey: cloudURL,
                      NSPersistentStoreUbiquitousContentNameKey: cloudStoreTitle};

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];


    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver:self
                   selector:@selector(processStoresWillChange:)
                       name:NSPersistentStoreCoordinatorStoresWillChangeNotification
                     object:_persistentStoreCoordinator];

    [notificationCenter addObserver:self
                   selector:@selector(processStoresDidChange:)
                       name:NSPersistentStoreCoordinatorStoresDidChangeNotification
                     object:_persistentStoreCoordinator];

    [notificationCenter addObserver:self
                   selector:@selector(processContentChanges:)
                       name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
                     object:_persistentStoreCoordinator];


    return _persistentStoreCoordinator;
}

在这两个应用程序中,启动时我都可以看到存储在iCloud驱动器的文档目录中的日志。有2个文件夹,1个与我的名字(mac)和一个与移动模拟器。当我在模拟器中创建新记录时,我可以看到在模拟器文件夹中创建的新事务日志。但是这个改变并没有反映在我的os x app上。我认为事务日志没有被复制到其他设备。每个设备仅侦听其文件夹中的更改。我怎么能这样做每个设备听别人的变化? 有没有办法将所有事务日志保存在所有设备的同一文件夹中?

运行os x app时出现此错误消息:

[PFUbiquitySetupAssistant canReadFromUbiquityRootLocation:]_block_invoke690(1489): CoreData: Ubiquity:  
Attempting to download Peers hit a serious error for peers to download Error Domain=BRCloudDocsErrorDomain Code=5 
No document at URL UserInfo=0x600000472f40 {NSDescription=No document at URL, 
NSFilePath=/Users/myName/Library/Mobile Documents/iCloud~myGroup~myApp/Documents/myAppCloudLogs1/.DS_Store, NSUnderlyingError=0x600000251340 } with userInfo {


NSDescription = "No document at URL";
NSFilePath = "/Users/myName/Library/Mobile Documents/iCloud~myGroup~myApp/Documents/myAppCloudLogs1/.DS_Store";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 UserInfo=0x600000472fc0 {NSDescription=No such file or directory}";
}

为什么我会收到此错误?什么是.DS_Store文件?在我的ICloud Drive文件夹中,我只看到.cdt文件。

请帮助。提前谢谢。

0 个答案:

没有答案