删除日志文件的最佳方法当从iCloud中删除UIManagedDocument时,Core Data会创建什么? [英] What is the best way to remove logs file Core Data creates, when removing a UIManagedDocument from iCloud?

查看:172
本文介绍了删除日志文件的最佳方法当从iCloud中删除UIManagedDocument时,Core Data会创建什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会认为 removeItemAtURL:error:的NSFileManager 使用UIManagedDocuments和iCloud。

I would have thought NSFileManagers method of removeItemAtURL:error: would remove the Core Data log files created when using UIManagedDocuments with iCloud.

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

What is the best way to make sure all of these log files are removed?

推荐答案

我使用了...

- (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存储第一次。

Note: this was used for a single document toolbox style app, and was intended more for clearing out the iCloud container before creating a brand new document, in an 'apparently' empty iCloud store for the first time. But I'm sure it can be adapted without too much work.

哎呀,上面的没有意义/工作没有:

Oops, the above won't make sense/work without:

typedef void (^completion_success_t)(BOOL success);

您可以调试iCloud容器的内容,并验证已通过使用这是真的我可能从别的地方取消和修改):

You can debug the contents of your iCloud container and verify things have been removed by using a method like (which to be honest I've probably lifted from somewhere else and modified):

- (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服务器文件夹结构中实际存在的内容之间存在差异。在所有这些之间,你可以很好地了解发生了什么。

And it's also worth logging onto developer.icloud.com and examining what is actually in the iCloud store. There is sometimes a difference between what is retained in the device ubiquity container, and what is actually in the iCloud server folder structure. Between all of these you can get quite a good idea of what's going on.

这篇关于删除日志文件的最佳方法当从iCloud中删除UIManagedDocument时,Core Data会创建什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆