ios中的清除缓存的做法

相当一部分的应用中是要做APP的清除缓存。具体代码如下:
1,获取缓存的目录方法
objc
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

2,获取一个对应目录的大小
```objc```

// 自己去计算SDWebImage做的缓存
+ (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger))completion
{
    // 获取文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    BOOL isDirectory;
    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    
    if (!isExist || !isDirectory) {
        // 抛异常
        // name:异常名称
        // reason:报错原因
        NSException *excp = [NSException exceptionWithName:@"pathError" reason:@"笨蛋 需要传入的是文件夹路径,并且路径要存在" userInfo:nil];
        [excp raise];
        
    }
    
//    开启子线程计算文件大小
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        // 获取文件夹下所有的子路径,包含子路径的子路径
        NSArray *subPaths = [mgr subpathsAtPath:directoryPath];
        
        NSInteger totalSize = 0;
        
        for (NSString *subPath in subPaths) {
            // 获取文件全路径
            NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
            
            // 判断隐藏文件
            if ([filePath containsString:@".DS"]) continue;
            
            // 判断是否文件夹
            BOOL isDirectory;
            // 判断文件是否存在,并且判断是否是文件夹
            BOOL isExist = [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];
            if (!isExist || isDirectory) continue;
            
            // 获取文件属性
            // attributesOfItemAtPath:只能获取文件尺寸,获取文件夹不对,
            NSDictionary *attr = [mgr attributesOfItemAtPath:filePath error:nil];
            
            // 获取文件尺寸
            NSInteger fileSize = [attr fileSize];
            
            totalSize += fileSize;
        }
        
        // 计算完成回调
        dispatch_sync(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(totalSize);
            }
        });   
    });
      
}

3,清除对应目录下的文件。
objc

  • (void)removeDirectoryPath:(NSString *)directoryPath
    {
    // 获取文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];

    BOOL isDirectory;
    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];

    if (!isExist || !isDirectory) {
    // 抛异常
    // name:异常名称
    // reason:报错原因
    NSException *excp = [NSException exceptionWithName:@"pathError" reason:@"笨蛋 需要传入的是文件夹路径,并且路径要存在" userInfo:nil];
    [excp raise];

    }

    // 获取cache文件夹下所有文件,不包括子路径的子路径
    NSArray *subPaths = [mgr contentsOfDirectoryAtPath:directoryPath error:nil];

    for (NSString *subPath in subPaths) {
    // 拼接完成全路径
    NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];

      // 删除路径
      [mgr removeItemAtPath:filePath error:nil];
    

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,033评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,868评论 18 139
  • 如果山里没有倔强 那绿草丛里 便会结满寒霜 群山变得凄凉 如果山里没有倔强 千年的古树 也会落下失落的泪光 凌霄花...
    小苹果的孤独阅读 126评论 0 0
  • 这是女儿说的命题。 《飞扬的杨絮》和《怀孕的母狗》 从娘家来的路上,杨絮满天,钻进车里,钻进鼻孔里。埋怨它们好是烦...
    夏花静秋阅读 343评论 0 0
  • 大家都知道,Git是非常强大的版本管理工具,今天就告诉大家,如何在Linux下安装GIt,并且做相关配置,与Git...
    Quenice阅读 3,199评论 0 4