【问题标题】:NSFileManager is not deleting files in temporary folderNSFileManager 没有删除临时文件夹中的文件
【发布时间】:2018-02-16 16:23:33
【问题描述】:

我正在使用一个应用程序,它从 Internet 下载图像并将它们缓存起来,以便在用户在图像之间来回滑动时获得更快的性能。

我想在 UIViewController 关闭时清除缓存文件夹。所以我创建了这个静态方法。但是,我不断收到错误消息:"File could NOT be deleted" on each file。

sDocumentsDir

/Users/MyName/Library/Developer/CoreSimulator/Devices/E5FAB592-143A-4B04-9A8C-CFEA73E6C712/data/Containers/Data/Application/2EB9CB73-9D34-4CE4-BA91-E9A3F2FC1CFA/Documents/。时间

文件名如下所示:

https___s3.amazonaws.com_mycompany_1288c8be6f8101.jpg

我做错了什么?

+ (BOOL) clearCacheFolder {
    NSFileManager *filemgr = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sDocumentsDir = [documentPaths objectAtIndex:0];
    sDocumentsDir = [sDocumentsDir stringByAppendingPathComponent:@"/.tmp"];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension == 'png' || pathExtension == 'jpg'"];

    BOOL isDir;
    if(([filemgr fileExistsAtPath:sDocumentsDir isDirectory:&isDir] && isDir)) {

        NSError *error;
        NSArray *fileArr = [filemgr contentsOfDirectoryAtPath:sDocumentsDir error:&error];
        if(error){
            NSLog(@"clearCacheFolder error populating array");
            return false;
        }

        NSLog(@"clearCacheFolder number of items to delete: %lu, files: %@",fileArr.count, fileArr.description);

        for(NSString *file in [fileArr filteredArrayUsingPredicate:predicate]){

            @try {
                [filemgr removeItemAtPath:[sDocumentsDir stringByAppendingString:file] error:&error];
            } @catch (NSException *exception) {
                NSLog(@"File delete exception: %@", exception.description);
            } @finally {
                if(error){
                    NSLog(@"File could NOT be deleted");
                } else {
                    NSLog(@"File Deleted");
                }
            }
        }

        fileArr = [filemgr contentsOfDirectoryAtPath:sDocumentsDir error:&error];
        NSLog(@"clearCacheFolder FINISHED count: %lu",fileArr.count);
    }
    return true;
}

【问题讨论】:

  • 您已经测试了 error 不是 nil,但您的代码似乎没有显示 error 是什么 - 这可能会对您有所帮助,请将其添加到您的 NSLog跨度>
  • 1.尝试使用缓存文件夹而不是文档
  • 2.如果您在 VC 出现时仅在短时间内使用缓存,我会建议您为其创建目录(您可以将其命名为 vc.hash,因为它是唯一的)和文件夹的全部内容,而不是复杂的过滤器。
  • @CRD(和 OP):在检查返回值之前,永远不要检查错误对象:重要提示:方法的返回值指示成功或失败。虽然间接返回 Cocoa 错误域中的错误对象的 Cocoa 方法如果通过直接返回 nil 或 NO 指示失败,则保证返回此类对象,但在尝试对NSError 对象。如果您提前处理了错误,代码可能会中断。
  • 此外,@try... 不是很可可。请阅读有关 Cocoa 中错误处理的内容。这和 CRD 的提示会有所帮助。

标签: ios objective-c nsfilemanager


【解决方案1】:

检查您尝试使用的文件路径。 [sDocumentsDir stringByAppendingString:file] 会产生错误的文件路径。 在您的示例中,它看起来像 /Users/MyName/Library/Developer/CoreSimulator/Devices/E5FAB592-143A-4B04-9A8C-CFEA73E6C712/data/Containers/Data/Application/2EB9CB73-9D34-4CE4-BA91-E9A3F2FC1CFA/Documents/.tmphttps___s3.amazonaws.com_mycompany_1288c8be6 jpg

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多