我有以下设置: 我的应用程序将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并再次包含已删除的文件。
我找不到原因。任何帮助表示赞赏。
答案 0 :(得分:0)
我遇到了类似的问题。从GCD中删除NSFileCoordinator
实例化的封装,然后你就可以执行删除操作了(尽管文档说它最好在后台线程中执行此操作,这很奇怪,因为这样做实际上是这样的首先导致这个无法删除的问题)
void (^deleteDocument)() = ^() {
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);
}
[_files removeObject:toDelete];
if (completion) {
completion((error == nil));
}
[self startQuery];
}];
};