ios清除缓存

    /*
   知识补充:应用沙盒(3部分)
       每个ios应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。
   1.Documents:可以将应用程序数据储存在Document目录中,如果应用启用了Itunes文件分享功能,Itunes同步设备时会备份该目录.
   2.Library:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录,一般储存体积大,不需要备份的数据.
   3.tmp:供应用储存临时文件,使用完毕后再将相应的文件从该目录删除,当ios设备同步时不会备份这个文件。

   模拟器沙盒目录:根目录
   NSString *homeDirectory = NSHomeDirectory();

   Documents路径:
   NSArray *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  firstObject];

    Library路径:
    NSArray *libraryDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];  
    Library--Cache目录(这里存放的就是缓存文件)
    NSArray *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; 
     Library--Cache--default(这里存放的是用户头像)
     NSArray *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"default"] ;

     tmp路径:
     NSString *tmpDir = NSTemporaryDirectory(); 
  */

    //获取缓存,清除缓存的操作比较耗时,理应开子线程去执行.
    //给NSObject封装一个分类,实现清除缓存的操作.

    //1.获取缓存
   //需要提供一个对象方法,外界去调用对象方法.
   - (void)getFileCacheSizeWithPath:(NSString *)path completion:(void (^)(NSInteger total))completion
   {
     [NSObject getFileCacheSizeWithPath:path completion:completion];
    }

   //写一个类方法
     // 异步方法使用回调,block
    + (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger total))completion
   {
     // 开启异步线程 2秒
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // 1.创建文件管理者   
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    // 1.1.判断下是否存在,而且是否是文件夹
    BOOL isDirectory; 
    BOOL isFileExist = [mgr fileExistsAtPath:path isDirectory:&isDirectory];
    
    // 判断下当前是否是文件
    if (isFileExist){
        // 判断下是否是文件夹
        NSInteger total = 0;
        if (isDirectory) {
            // 2.遍历文件夹下所有文件,全部加上,就是文件夹大小
            NSArray *subPaths = [mgr subpathsAtPath:path];
            for (NSString *subPath in subPaths) {
                // 3.拼接文件全路径
                NSString *filePath = [path stringByAppendingPathComponent:subPath];
                BOOL isDirectory;
                // 4.判断下当前是否是文件
                BOOL isFileExist = [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];
                // 5.获取文件大小
                if (isFileExist && !isDirectory && ![filePath containsString:@"DS"]) {
                    
                    NSDictionary *fileAttr = [mgr attributesOfItemAtPath:filePath error:nil];
                    NSInteger fileSize = [fileAttr[NSFileSize] integerValue];
                    total += fileSize;
                } 
            }
            
        }else{
            // 当前传入是文件
            NSDictionary *fileAttr = [mgr attributesOfItemAtPath:path error:nil];
            total = [fileAttr[NSFileSize] integerValue];
        }
        
        // 计算完毕 -> 把计算的值传递出去
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            if (completion) {
                completion(total);
            }
        });
    }
    });

   }

  //2.清除缓存
  + (void)removeCacheWithCompletion:(void (^)())completion
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // 创建文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];  
    // 删除文件
    NSString *path = self.cachePath;
    BOOL isDirectory;
    BOOL isFileExist = [mgr fileExistsAtPath:path isDirectory:&isDirectory];
    if (!isFileExist) return;
    if (isDirectory) {
        NSArray *subPaths = [mgr subpathsAtPath:path];
        for (NSString *subPath in subPaths) {
            NSString *filePath = [path stringByAppendingPathComponent:subPath];
            [mgr removeItemAtPath:filePath error:nil];
        }
    }

     dispatch_sync(dispatch_get_main_queue(), ^{
        if (completion) {
            completion();
        }
    });
});

}

- (void)removeCacheWithCompletion:(void (^)())completion
 {
   [NSObject removeCacheWithCompletion:completion];

  }

 // 3.缓存路径
 - (NSString *)cachePath
 {
     return [NSObject cachePath];
 }

 + (NSString *)cachePath
   {
     // 获取cachePath文件路径
      return  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
     }
    
    // 写的够详细了吧,至于外界怎么去调用,废话不多说了.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.点击单元格弹出提示 NSString *str = [self countCacheSize]; NSStri...
    滴答大阅读 2,463评论 0 3
  • float tmpSize = [[SDImageCache sharedImageCache] getSize]...
    赵永洪阅读 3,853评论 0 1
  • 应用场景: 现在众多app中都会有清楚缓存的功能,怎么能精确的计算缓存文件的大小,从而清除缓存文件呢,下面对清楚功...
    Mr_Bob_阅读 5,668评论 7 19
  • 在我们开发app的过程中,都会涉及到一个问题,那就是清除缓存,我们都知道,随着应用使用频率的增加,程序cache的...
    某某cp阅读 3,954评论 0 1
  • 简述:下面的是根据SDImage图片缓存与文件缓存结合在一起来写的需要导入SDImage三方,一般需要导入头文件#...
    IIronMan阅读 3,698评论 0 6