0

我原以为NSFileManagers 方法removeItemAtURL:error:会删除在将 UIManagedDocuments 与 iCloud 一起使用时创建的核心数据日志文件。

确保删除所有这些日志文件的最佳方法是什么?

4

1 回答 1

0

我用过...

- (void)deleteRemnantsOfOldDatabaseDocumentAndItsTransactionLogsWithCompletionHandler:(completion_success_t)completionBlock
{    
    __weak CloudController *weakSelf = self;

    NSURL *databaseStoreFolder = self.iCloudDatabaseStoreFolderURL;
    NSURL *transactionLogFolder = self.transactionLogFilesFolderURL;

    [self deleteFileAtURL:databaseStoreFolder withCompletionBlock:^(BOOL docSuccess) {
        [weakSelf deleteFileAtURL:transactionLogFolder withCompletionBlock:^(BOOL logSuccess) {
            completionBlock(docSuccess && logSuccess);
        }];
    }];
}

和这个结合...

- (void)deleteFileAtURL:(NSURL *)fileURL withCompletionBlock:(completion_success_t)completionBlock
{    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
        NSError *coordinatorError = nil;
        __block BOOL success = NO;

        [fileCoordinator coordinateWritingItemAtURL:fileURL
                                            options:NSFileCoordinatorWritingForDeleting
                                              error:&coordinatorError
                                         byAccessor:^(NSURL *writingURL) {

                                             NSFileManager *fileManager = [[NSFileManager alloc] init];
                                             NSError *removalError = nil;
                                             if ([fileManager fileExistsAtPath:[writingURL path]]) {
                                                 if (![fileManager removeItemAtURL:writingURL error:&removalError]) {
                                                     NSLog(@"deleteFileAtURL: removal error: %@", removalError);
                                                 } else {
                                                     success = YES;
                                                 }
                                             } 
                                         }];

        if (coordinatorError) {
            NSLog(@"deleteFileAtURL: coordinator error: %@", coordinatorError);
        }

        completionBlock(success);
    });
}

注意:这用于单个文档工具箱样式的应用程序,并且更多地用于在创建全新文档之前清除 iCloud 容器,这是第一次在“显然”空的 iCloud 存储中。但我确信它可以在没有太多工作的情况下进行调整。

糟糕,如果没有以下内容,上述内容将毫无意义/工作:

typedef void (^completion_success_t)(BOOL success);

您可以调试 iCloud 容器的内容并使用以下方法验证内容是否已删除(老实说,我可能已经从其他地方提取并修改了):

- (void)logDirectoryHierarchyContentsForURL:(NSURL *)url
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtURL:url
                                                   includingPropertiesForKeys:@[NSURLNameKey, NSURLContentModificationDateKey]
                                                                      options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                 errorHandler:nil];

    NSMutableArray *results = [NSMutableArray array];

    for (NSURL *itemURL in directoryEnumerator) {
        NSString *fileName;
        [itemURL getResourceValue:&fileName forKey:NSURLNameKey error:NULL];

        NSDate *modificationDate;
        [itemURL getResourceValue:&modificationDate forKey:NSURLContentModificationDateKey error:NULL];

        [results addObject:[NSString stringWithFormat:@"%@ (%@)", itemURL, modificationDate]];
    }

    NSLog(@"Directory contents: %@", results);
}

还值得登录developer.icloud.com并检查 iCloud 商店中的实际内容。设备普遍存在容器中保留的内容与 iCloud 服务器文件夹结构中实际保留的内容有时会有所不同。在所有这些之间,您可以很好地了解正在发生的事情。

于 2013-05-18T09:48:51.287 回答