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;
        }
        
        // 計算完成回調(diào)
        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];
    

    }

}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,761評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 如果山里沒有倔強 那綠草叢里 便會結(jié)滿寒霜 群山變得凄涼 如果山里沒有倔強 千年的古樹 也會落下失落的淚光 凌霄花...
    小蘋果的孤獨閱讀 154評論 0 0
  • 這是女兒說的命題。 《飛揚的楊絮》和《懷孕的母狗》 從娘家來的路上,楊絮滿天,鉆進車里,鉆進鼻孔里。埋怨它們好是煩...
    夏花靜秋閱讀 382評論 0 0
  • 大家都知道,Git是非常強大的版本管理工具,今天就告訴大家,如何在Linux下安裝GIt,并且做相關配置,與Git...
    Quenice閱讀 3,258評論 0 4

友情鏈接更多精彩內(nèi)容