如何计算及清除缓存

  • 首先计算缓存值的大小
//计算缓存值得大小
            float tmpSize = 0;
            NSString *diskCachePath= [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSDirectoryEnumerator *fileEnumrator = [[NSFileManager defaultManager] enumeratorAtPath:diskCachePath];
            for (NSString *fileName in fileEnumrator) {
                NSString *filePath = [diskCachePath stringByAppendingPathComponent:fileName];
                NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
                unsigned long long length = [attrs fileSize];
                tmpSize += length/1024.0/1024.0;
            }
            NSLog(@"tmp size is %.2f",tmpSize);
            self.tempLabel.text = [NSString stringWithFormat:@"%.2fM",tmpSize];
  • 清除缓存
 //执行清除缓存操作.放在子线程中进行.
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                NSLog(@"files:%ld",(unsigned long)[files count]);
                
                for (NSString *p in files) {
                    NSError *error;
                    NSString *path = [cachPath stringByAppendingPathComponent:p];
                    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                    }
                }
                [self performSelectorOnMainThread:@selector(clearCachesSuccess) withObject:nil waitUntilDone:YES];
            });
//清楚缓存
- (void)clearCachesSuccess{
    
    [self myAlertMessage:@"是否清除缓存"];
}
#pragma mark------提示框
- (void)myAlertMessage:(NSString*)message{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message: message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        // 开始清空
        self.tempLabel.text = @"";
        NSLog(@"清除成功");
    }];
    UIAlertAction *cancelACtion = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleDefault handler:nil];
    [alertController addAction:sureAction];
    [alertController addAction:cancelACtion];
    [self presentViewController:alertController animated:YES completion:nil];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 9,201评论 2 7
  • 27、ViewController的didReceiveMemoryWarning是在什么时候调用的?默认的操作是...
    烟雨平生花飞舞阅读 3,811评论 0 1
  • 昨天一天下午还真是个种查,各种搜索,然后各种技术群,各种问,(没人鸟我),其实我是有这个能力的,怎么就一上...
    梦随兴飞阅读 25,239评论 7 18
  • # ios常用的一些判断 ## 根据日起计算是周几 ``` + (NSString*)weekdayStringF...
    呵呵你妹521阅读 4,581评论 1 4
  • 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。应用必须待在自己的沙盒里,其他应...
    彬至睢阳阅读 4,014评论 0 0

友情链接更多精彩内容