我有以下设置:我的应用程序将 UIDocument 实例写入 iCloud。文件同步,一切正常。但是当我尝试删除它们时,它们会不断重新出现。在我删除文件之前,我关闭它们。这是删除文件的代码:
- (void)deleteDocumentWithName:(NSString*)name completion:(void (^)(BOOL success))completion
{
[self stopQuery];
NSURL* toDelete = [self URLForFileWithName:name];
UIDocument* doc = [[UIDocument alloc] initWithFileURL:toDelete];
void (^deleteDocument)() = ^() {
// Wrap in file coordinator
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateWritingItemAtURL:toDelete
options:NSFileCoordinatorWritingForDeleting
error:nil
byAccessor:^(NSURL* writingURL) {
// Simple delete to start
NSFileManager* fileManager = [[NSFileManager alloc] init];
NSError* error = nil;
[fileManager removeItemAtURL:writingURL error:&error];
if (error) {
LogError(@"%s - error while deleting file: %@", __PRETTY_FUNCTION__, error);
}
dispatch_async(dispatch_get_main_queue(), ^{
[_files removeObject:toDelete];
if (completion) {
completion((error == nil));
}
[self startQuery];
});
}];
});
};
if (doc) {
if (doc.documentState == UIDocumentStateNormal) {
[doc closeWithCompletionHandler:^(BOOL success) {
if (success) {
deleteDocument();
} else {
if (completion) {
completion(NO);
}
}
}];
} else {
deleteDocument();
}
}
}
执行此方法后,我得到了几个 NSMetadataQueryDidFinishGatheringNotifications。首先它不包含已删除的文件。然后另一个 NSMetadataQueryDidFinishGatheringNotification 被捕获并再次包含已删除的文件。
我找不到这个的原因。任何帮助表示赞赏。