2

我正在尝试使用[NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:options:error:]. 但我得到奇怪的输出:

__93+[NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:options:error:]_block_invoke(1982):
CoreData: Ubiquity:  Unable to move content directory to new location:
file:///private/var/mobile/Library/Mobile%20Documents/<UBIQUITY_ID>/    
New: file:///private/var/mobile/Library/Mobile%20Documents/OldUbiquitousContent-mobile~C9439AD0-1E87-4977-9C68-0674F5E2E93B
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed.
(Cocoa error 513.)" UserInfo=0x181ab790 {NSSourceFilePathErrorKey=/private/var/mobile/Library/Mobile Documents/<UBIQUITY_ID>,     
NSUserStringVariant=(
    Move
), NSFilePath=/private/var/mobile/Library/Mobile Documents/<UBIQUITY_ID>,
NSDestinationFilePath=/private/var/mobile/Library/Mobile Documents/OldUbiquitousContent-mobile~C9439AD0-1E87-4977-9C68-0674F5E2E93B,
NSUnderlyingError=0x181aab50 "The operation couldn’t be completed. Operation not permitted"}

这是什么意思?

如何避免?我正在研究 iCloud 禁用/启用功能。详情在这里

更新:

  NSDictionary *iCloudOptions =
  [NSDictionary dictionaryWithObjectsAndKeys:kICloudContentNameKey, NSPersistentStoreUbiquitousContentNameKey,
   iCloudURL, NSPersistentStoreUbiquitousContentURLKey, nil];

// self.lastICloudStoreURL stores NSPersistentStore.URL after stack setup
  BOOL result = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:self.lastICloudStoreURL
                                                                                     options:iCloudOptions
                                                                                       error:&error];
4

3 回答 3

2

通常(在 iOS7 之前),您从 ubiquitousContentURL 中获取值[fileManager URLForUbiquityContainerIdentifier:nil]; 并将其作为名为 的选项传递NSPersistentStore UbiquitousContentURLKey,这就是 iCloud 知道将所有数据保存在 iCloud 帐户中的位置的方式。

在 iOS 7 和 Mac OS X 中,我们根本不需要为此传递值,Apple 会自动为您调用 URLForUbiquitous ContainerIdentifier。

所以解决方案如下所示:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:kICloudContentNameKey, NSPersistentStoreUbiquitousContentNameKey, nil];
NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:[MagicalRecord defaultStoreName]];
BOOL result = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL options:options error:&error];

我建议您查看 WWDC 2013 session 207 以清楚地了解这些内容。

于 2014-08-06T14:05:57.890 回答
1

NSCocoaErrorDomain 错误 513 在 FoundationErrors.h 中定义为NSFileWriteNoPermissionError. 您没有写入该位置所需的权限。

如果托管对象上下文仍在使用此存储支持的对象,则可能会发生这种情况。上下文正在积极使用正在移动的文件,这会导致 NSFileCoordinator 冲突。有两件事试图同时访问具有写权限的文件。

于 2014-08-05T20:00:25.673 回答
0

removeUbiquitousContentAndPersistentStoreAtURL:options:error:方法会删除所有用户的本地和云数据——这可能不是您想要的。相反,将您的商店迁移到磁盘上的新位置并使用该方法的NSPersistentStoreRemoveUbiquitousMetadataOption选项migratePersistentStore:toURL:options:withType:error:

请参阅iCloud Programming Guide for Core Data中的Disabling iCloud Persistence at Removing an iCloud-enabled Persistent Store

于 2014-08-05T20:41:49.840 回答