美文网首页
一行代码搞定清除缓存

一行代码搞定清除缓存

作者: HJXu | 来源:发表于2016-07-13 16:46 被阅读101次

    现在很多app都有清除缓存的功能,下面是之前封装好的全部代码,只需要一句话就可以搞定清除缓存
    .h文件

    // 文件操作以及属性获取等方法
    #import <Foundation/Foundation.h>
    
    @interface NSObject (FileManager)
    
    /*  计算某个文件或者文件夹的大小
        path:文件或者文件夹路径
        completion:计算完成回调,计算完成就会调用这个block,并且把计算的大小传出去
     */
    + (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger totalSize))completion;
    
    - (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger totalSize))completion;
    
    // 获取沙盒中cache文件夹的路径
    - (NSString *)cachePath;
    + (NSString *)cachePath;
    
    // 删除沙盒中cache文件夹里面的缓存(文件)
    - (void)removeCacheWithCompletion:(void(^)())completion;
    + (void)removeCacheWithCompletion:(void(^)())completion;
    
    @end
    
    

    .m文件

    #import "NSObject+FileManager.h"
    
    @implementation NSObject (FileManager)
    
    - (NSString *)cachePath
    {
        return [NSObject cachePath];
    }
    
    + (NSString *)cachePath
    {
        // 获取cachePath文件路径
        return  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    }
    
    - (void)getFileCacheSizeWithPath:(NSString *)path completion:(void (^)(NSInteger))completion
    {
        [NSObject getFileCacheSizeWithPath:path completion:completion];
    }
    
    // 异步方法,不需要返回值,异步方法使用回调block传递返回值
    // 获取文件尺寸
    + (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger totalSize))completion
    {
        
        // 文件操作比较耗时,开启子线程去操作,然后在返回
        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){
                unsigned long long folderSize = 0;
                // 判断下是否是文件夹
                if (isDirectory) {
                    // 方式一:
                    NSEnumerator *childFilesEnum = [[mgr subpathsAtPath:path] objectEnumerator];
                    NSString* fileName;
                    while ((fileName = [childFilesEnum nextObject]) != nil){
                        NSString* fileAbsPath = [path stringByAppendingPathComponent:fileName];
                        if ([mgr fileExistsAtPath:fileAbsPath isDirectory:&isDirectory] && !isDirectory) {
                            folderSize += [mgr attributesOfItemAtPath:fileAbsPath error:nil].fileSize;
                        }
                    }
                    
    //                // 方式二:
    //                NSDirectoryEnumerator *dirEnum = [mgr enumeratorAtPath:path];
    //                for (NSString *subpath in dirEnum) {
    //                    NSString *fullPath = [path stringByAppendingPathComponent:subpath];
    ////                    BOOL isDir;
    //                    // 如果是文件夹的话就不计算大小,attributesOfItemAtPath:error:方法会计算文件夹的大小
    //                    if ([mgr fileExistsAtPath:fullPath isDirectory:&isDirectory] && !isDirectory) {
    //                        folderSize += [mgr attributesOfItemAtPath:fullPath error:nil].fileSize;
    //                    }
    //                }
                }else{
                    // 当前传入是文件
                    folderSize = [mgr attributesOfItemAtPath:path error:nil].fileSize;
                }
    
                // 计算完毕 -> 把计算的值传递出去
                dispatch_sync(dispatch_get_main_queue(), ^{
                    if (completion) {
                        completion(folderSize);
                    }
                });
            }
        });
    }
    
    + (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];
        
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:一行代码搞定清除缓存

          本文链接:https://www.haomeiwen.com/subject/wpzsjttx.html