从iCloud中删除核心数据失败
我试图使用[NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:options:error:]
从iCloud中删除核心数据。 但是我得到奇怪的输出:
__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禁用/启用function。 详情在这里
更新:
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];
通常(在iOS7之前),你从[fileManager URLForUbiquityContainerIdentifier:nil];
取得ubiquitousContentURL值[fileManager URLForUbiquityContainerIdentifier:nil];
并将其作为名为NSPersistentStore UbiquitousContentURLKey
的选项NSPersistentStore UbiquitousContentURLKey
,这就是iCloud知道如何将所有数据保存在iCloud帐户中的方式。
在iOS 7和Mac OS X中,我们不需要为此传递一个值,苹果会为您自动调用URLForUbiquitous ContainerIdentifier。
所以解决scheme如下所示:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:kICloudContentNameKey, NSPersistentStoreUbiquitousContentNameKey, nil]; NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:[MagicalRecord defaultStoreName]]; BOOL result = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL options:options error:&error];
我build议您查看WWDC 2013会话207以清楚地了解这些事情。
NSCocoaErrorDomain错误513在FoundationErrors.h中定义为NSFileWriteNoPermissionError
。 您没有写入该位置所需的权限。
如果托pipe对象上下文仍在使用由此存储支持的对象,则可能发生这种情况。 上下文正在使用正在被移动的文件,这会导致NSFileCoordinator冲突。 有两件事是试图同时访问具有写入权限的文件。
removeUbiquitousContentAndPersistentStoreAtURL:options:error:
方法删除所有用户的本地和云数据 – 这可能不是你想要的。 而是将您的商店迁移到磁盘上的新位置,并使用NSPersistentStoreRemoveUbiquitousMetadataOption
选项和migratePersistentStore:toURL:options:withType:error:
方法。
请参阅“ iCloud核心数据编程指南”中的“ 删除启用iCloud的持久性存储”时 禁用iCloud持久性 。