【问题标题】:What is the best way to remove logs file Core Data creates, when removing a UIManagedDocument from iCloud?从 iCloud 中删除 UIManagedDocument 时,删除 Core Data 创建的日志文件的最佳方法是什么?
【发布时间】:2013-04-30 09:52:36
【问题描述】:

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

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

【问题讨论】:

    标签: core-data icloud nsfilemanager


    【解决方案1】:

    我用过……

    - (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 服务器文件夹结构中实际保留的内容有时会有所不同。在所有这些之间,您可以很好地了解正在发生的事情。

    【讨论】:

      猜你喜欢
      • 2010-10-16
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多