У меня есть следующая настройка: Мое приложение записывает экземпляры 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, который снова содержит удаленный файл.
Я не могу найти причину этого. Любая помощь приветствуется.
The receiver first completes gathering any unprocessed results.
- person Rob Glassey   schedule 20.08.2013UIDocument
закрыта и освобождена, не только решила проблему повторного появления файла, но и нетривиальную проблему увеличения объема памяти в моем приложении. Спасибо! - person HuaTham   schedule 09.01.2018