I have the following setup: My app writes UIDocument instances to iCloud. The files get synced and everything works fine. But when I try to delete them they keep reappearing. Before I delete the files I close them. Here is the code that deletes the files:
- (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();
}
}
}
After this method was executed I get several NSMetadataQueryDidFinishGatheringNotifications. First it does not contain the deleted file. Then another NSMetadataQueryDidFinishGatheringNotification gets caught and it contains the deleted file again.
I can't find the reason for this. Any help is appreciated.