3

我有以下设置:我的应用程序将 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 被捕获并再次包含已删除的文件。

我找不到这个的原因。任何帮助表示赞赏。

4

1 回答 1

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];
        }];
};
于 2017-05-06T10:17:49.550 回答