Core data 删除UIManagedDocument的正确方法是什么?

Core data 删除UIManagedDocument的正确方法是什么?,core-data,icloud,uimanageddocument,Core Data,Icloud,Uimanageddocument,我似乎找不到任何可靠的文档来解释删除UIManagedDocument的正确过程,尤其是在iCloud选项已打开的文档中 我知道此选项将删除此文件URL处的文件。如果不使用iCloud,这似乎没什么问题 [[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error]; 如果正在使用iCloud,则CoreData会在所有地方创建文件,包括在/Document/CoreDataUbiquitySupport和iC

我似乎找不到任何可靠的文档来解释删除UIManagedDocument的正确过程,尤其是在iCloud选项已打开的文档中

我知道此选项将删除此文件URL处的文件。如果不使用iCloud,这似乎没什么问题

[[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];
如果正在使用iCloud,则CoreData会在所有地方创建文件,包括在/Document/CoreDataUbiquitySupport和iCloud/CoreData文件夹中。因此,在这种情况下,在调用
[NSFileManager removietematural]
之前,是否由我为
UIManagedDocument
中的每个存储调用
RemoveUbiquiousContentandPersistentStoreatural
。如果是这样的话,是否有文件记录

[NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL
                                                                     options:@{NSPersistentStoreUbiquitousContentNameKey:fileName,
   NSMigratePersistentStoresAutomaticallyOption:@YES,
         NSInferMappingModelAutomaticallyOption:@YES,
                          NSSQLitePragmasOption:@{ @"journal_mode" : @"DELETE" }}
                                                                       error:&error];

对于iCloud核心数据内容,您希望在
NSPersistentStoreCoordinator
类上调用静态方法
RemoveUbiquiousContentandPersistentStoreAttribute:options:error:
,然后调用
RemoveItemAttribute:error:


请参见我的项目中的
deleteManagedDocumentWithIdentifier:
。这是关于
无所不在的分支的,在我将其合并回主分支之前,我目前正在完成该分支。

这是我关于该问题的两分钱。我尝试了dtrotzjr推荐的方法,但没有取得很大的成功。似乎RemoveUbiquiousContentandPersistentStore属性:选项:错误:对于清除UIManagedDocument中的数据非常有用,但是Logs文件夹仍然存在,我试图删除的文件的剩余部分也仍然存在。以下是从iCloud或本地文档中完全删除UIManagedDocument的更简单方法:

+ (void)deleteDocumentURL:(NSURL *)url{
    //if we have an iCloud Document, remove it from the UbiquitouseKeyValueStore
    if ([self isiCloudURL:url]) {
        [[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:[url lastPathComponent]];
    }

    //do the delete on another thread
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
        NSError *coordinationError;

        [coordinator coordinateWritingItemAtURL:url
                                    options:NSFileCoordinatorWritingForDeleting
                                      error:&coordinationError
                                 byAccessor:^(NSURL *newURL) {
         NSError *removeError;
        //code for performing the delete
        [[NSFileManager defaultManager] removeItemAtURL:newURL error:&removeError];

        //if we have an iCloud file...
        if ([self isiCloudURL:url]) {
            //remove log files in CoreData directory in the cloud
            NSURL *changeLogsURL = [[self urlForiCloudLogFiles] URLByAppendingPathComponent:[url lastPathComponent]];
                [[NSFileManager defaultManager] removeItemAtURL:changeLogsURL error:&removeError];
            }
        }];
    });
}

这几乎是斯坦福CS193课程2012+changeLogs文件夹删除的代码,它适用于本地和iCloud文档。如果您发现以这种方式执行删除有任何问题,请告诉我。

这是否也应该从其他设备上删除该文件,或者我是否应该在其他设备上的应用程序中有逻辑来检测该文件不再在iCloud中,并在该设备上执行相同的操作?检测文件已被删除的逻辑是什么?苹果的任何文档?调用
removeUbiquiousContentandPersistentStore自然:选项:错误
将从iCloud中删除核心数据事务日志并删除永久存储文件,调用
removeItem自然:错误
将删除
UIManagedDocument
包。现在我考虑一下,因为我将文档存储在泛在存储中,所以我需要先
设置泛在:NO
文档,这样iCloud也会忘记这一点。请参阅WWDC 2013会话207,了解有关
removeUbiquiousContentandPersistentStore特性的更多信息:选项:错误
Sure但当应用程序在其他设备上启动时,其他设备会发生什么情况?考虑到他们可能还有一个数据库的工作副本,我没有看到核心数据文档中有通知告诉我们“哦,有人删除了云中的所有内容,你现在想做什么:a)构建一个新副本并将其放回云中,b)保留副本并使其仅为本地,c)也删除您的副本或d)让我们决定?“这真是太奇怪了——现在我的mac电脑也在线了!!这是胡说八道,我只是打开我现有的sqlite文件,然后在文件上运行migrate,也就是说,它们在云中,它们自己甚至不移动到任何地方。干了,卡普!太容易了!我确实必须关闭沙盒才能打开桌面上的任何文件。Core Data在与文档相同的目录中创建一个CoreDataUbiquitySupport文件夹,该文件夹包含后备存储和其他内容。当然他们不可能这么简单。你看了WWDC的视频了吗?当persistentStore将被删除或更改且应用程序仍在运行时,他们会进行回调。请查看此链接,我在其中发布了一些代码示例和演示视频,介绍了我如何使其非常可靠地工作。它显示了正在创建和清除的本地目录和iCloud目录。很酷,谢谢Duncan。此文档看起来很有希望:)