一行代碼搞定清除緩存

現在很多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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容