我有一个适用于 OSX 和 iOS 的应用程序,两者都使用 iCloud 在它们之间共享核心数据。当我在 iPhone 中进行更改时,我可以在 OSX 应用程序中看到它们,并且 NSPersistentStoreDidImportUbiquitousContentChangesNotification
在两个应用程序中都被调用,因此 iOS -> Mac 运行良好。但是当我在 Mac 应用程序中进行一些更改时,我无法在 iPhone 中看到它们。 iPhone 从不调用 NSPersistentStoreDidImportUbiquitousContentChangesNotification
。我能够获得 Mac 更改的唯一方法是在 iPhone 上进行一些更改。然后我可以看到所有的变化。
两个项目中的 iCloud 集成是相同的。核心数据数据模型是相同的。权利也是相同的。
为什么在我对 iPhone 进行一些更改之前,iPhone 没有获得 Mac 更改?有什么想法吗?
另一个问题
在我的应用程序中,当用户启用 iCloud 时,我会检查是否已经是 iCloud 上的文件。如果文件存在,用户可以选择从 iCloud 文件恢复核心数据或从本地存储开始新的副本。 要开始新副本,我使用以下代码:
- (void)startNewCopy
{
[self removeICloudStore];
[self moveStoreToIcloud];
}
- (void)removeICloudStore
{
BOOL result;
NSError *error;
// Now delete the iCloud content and file
result = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:[self icloudStoreURL]
options:[self icloudStoreOptions]
error:&error];
if (!result)
{
NSLog(@"Error removing store");
}
else
{
NSLog(@"Core Data store removed.");
// Now delete the local file
[self deleteLocalCopyOfiCloudStore];
}
}
- (void)deleteLocalCopyOfiCloudStore {
// We need to get the URL to the store
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:[self localUbiquitySupportURL] error:&error];
}
- (void)moveStoreToIcloud
{
// Open the store
NSPersistentStore *sourceStore = [[_persistentStoreCoordinator persistentStores] firstObject];
if (!sourceStore)
{
NSLog(@" failed to add old store");
}
else
{
NSLog(@" Successfully added store to migrate");
NSError *error;
id migrationSuccess = [_persistentStoreCoordinator migratePersistentStore:sourceStore toURL:[self icloudStoreURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error];
if (migrationSuccess)
{
NSLog(@"Store migrated to iCloud");
_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
// Now delete the local file
[self deleteLocalStore];
}
else
{
NSLog(@"Failed to migrate store: %@, %@", error, error.userInfo);
}
}
}
- (void)deleteLocalStore
{
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:[self localStoreURL] error:&error];
}
在单个设备上执行此操作似乎一切正常,但当我在多个设备上尝试此操作时,我遇到了一些错误。
例子:
我有两个设备。第一个没有连接到 iCloud,第二个连接到 iCloud。
在第一台设备中,当我启用 iCloud 时,我选择 startNewCopy
,然后在第二台设备中出现此错误:
CoreData: Ubiquity: Librarian returned a serious error for starting downloads Error Domain=BRCloudDocsErrorDomain Code=5 "The operation couldn’t be completed. (BRCloudDocsErrorDomain error 5 - No document at URL)"
NSPersistentStoreCoordinatorStoresDidChangeNotification
从未在错误发生前被调用。
这是我的通知代码:
- (void)processStoresDidChange:(NSNotification *)notification
{
NSLog(@"processStoresDidChange");
// Post notification to trigger UI updates
// Check type of transition
NSNumber *type = [notification.userInfo objectForKey:NSPersistentStoreUbiquitousTransitionTypeKey];
//NSLog(@" userInfo is %@", notification.userInfo);
//NSLog(@" transition type is %@", type);
if (type.intValue == NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted) {
NSLog(@" transition type is NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted");
} else if (type.intValue == NSPersistentStoreUbiquitousTransitionTypeAccountAdded) {
NSLog(@" transition type is NSPersistentStoreUbiquitousTransitionTypeAccountAdded");
} else if (type.intValue == NSPersistentStoreUbiquitousTransitionTypeAccountRemoved) {
NSLog(@" transition type is NSPersistentStoreUbiquitousTransitionTypeAccountRemoved");
} else if (type.intValue == NSPersistentStoreUbiquitousTransitionTypeContentRemoved) {
NSLog(@" transition type is NSPersistentStoreUbiquitousTransitionTypeContentRemoved");
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
if (type.intValue == NSPersistentStoreUbiquitousTransitionTypeContentRemoved) {
[self deleteLocalStore];
_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
NSLog(@" iCloud store was removed! Wait for empty store");
}
// Refresh user Interface
[[NSNotificationCenter defaultCenter] postNotificationName:@"notiIcloud" object:@"storeChanged"];
}];
}
如何检测到 iCloud 内容已被删除并避免出现上述错误?
最佳答案
在我看来,您可能没有使用适当的接口(interface)。
您可以在不使用 NSPersistentStore
方法的情况下将文件移入和移出 iCloud。
通常还必须将适当的操作包装在 NSFileCoordinator
调用中。
这是我在 iOS 上所做的。 toLocal
表示移动到本地(设备)存储或从本地(设备)存储移动:
//
/// Move file between stores (assumed no name clash).
/// Return new URL.
/// Note: cloud file moves are asynchronous.
///
- (NSURL *)moveStoreFile:(NSURL *)url toRootURL:(NSURL *)rootURL toLocal:(BOOL)toLocal
{
NSURL *newURL = rootURL;
if (!toLocal)
newURL = [newURL URLByAppendingPathComponent:@"Documents"];
newURL = [newURL URLByAppendingPathComponent:url.lastPathComponent];
[self backupFile:url isLocal:!toLocal completionHandler:
^(BOOL success) {
MRLOG(@"MOVE %s %@ to %s %@", toLocal? "icloud" : "local", [url lastPathComponent],
toLocal? "local" : "icloud", [newURL lastPathComponent]);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
NSError *error;
// setUbiquitous does file coordination
BOOL msuccess = [[NSFileManager defaultManager]
setUbiquitous:!toLocal itemAtURL:url destinationURL:newURL error:&error];
dispatch_async(dispatch_get_main_queue(),
^{
if (!msuccess)
MRLOG(@"move failed: %@", error);
[self.delegate moveStoreFileCompletedWithStatus:success error:error];
});
});
}];
return newURL;
}
删除需要明确的文件协调:
///
/// Purge backup file (really delete it).
/// Note: cloud deletes are asynchronous.
///
- (void)purgeFile:(NSURL *)url isLocal:(BOOL)isLocal
{
MRLOG(@"PURGE %s %@", isLocal? "local" : "icloud", [url lastPathComponent]);
if (isLocal)
[self removeFile:url];
else
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
NSError *coordinationError;
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[coordinator coordinateWritingItemAtURL:url options:NSFileCoordinatorWritingForDeleting
error:&coordinationError
byAccessor:
^(NSURL* writingURL)
{
[self removeFile:writingURL];
}];
if (coordinationError) {
MRLOG(@"coordination error: %@", coordinationError);
[(SSApplication *)[SSApplication sharedApplication] fileErrorAlert:coordinationError];
}
});
}
要检测文件更改,您需要使用 NSMetadataQuery
并为 NSMetadataQueryDidUpdateNotification
添加观察者。
关于ios - OSX 和 iOS 之间的 iCloud 核心数据共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330863/