給App新增快取功能 清除快取功能

2021-07-16 07:04:22 字數 1525 閱讀 5170

(1)寫成檔案,用text或plist檔案進行資料儲存

1.寫乙個工具類,用以獲取檔案的路徑

/**

* 獲取documents資料夾路徑

* * @return 資料夾路徑

*/+ (nsstring *)getdocumentsfolderpath

/** * 獲取documents檔案路徑

* * @param filename 檔名稱

* * @return 檔案路徑

*/+ (nsstring *)getdocumentsfilepath:(nsstring *)filename

2.在網路成功請求資料後,將資料存起來,寫成檔案

//datadict 是網路請求回來的資料字典

//這裡是呼叫上面工具類的方法,獲取檔案路徑(自定義檔案的名字)

nsstring *path = [pathservice getdocumentsfilepath:@"data.plist"];

[datadict writetofile:path atomically:yes];

nsdictionary *attributes = [nsdictionary dictionarywithobject:nsfileprotectioncomplete

forkey:nsfileprotectionkey];

[[nsfilemanager defaultmanager] setattributes:attributes

ofitematpath:path

error:nil];

nsstring *systempath = [pathservice getdocumentsfilepath:@"data.plist"];

//這裡的字典就是之前網路請求成功的資料

nsdictionary *systemdata = [nsdictionary dictionarywithcontentsoffile:systempath];

(2)第一種是簡單的對請求的資料進行儲存,如果需要對不同使用者做不同的快取,搭建資料庫進行資料快取

使用fmdb或者coredata搭建資料庫,這裡不詳細介紹。

(3)快取清除

//把要清除的檔名寫到這,如果全部都清除,遍歷快取資料夾,全部清除即可。

nsarray *patharray = @[@"normalbidlistinfo.txt",@"activitybidlistinfo.txt"];

for (nsstring *pathname in patharray)

nserror *err;

bool deletesuccess = [manager removeitematpath:path error:&err];

if (!deletesuccess)

資料庫的快取清除,呼叫資料庫執行清除表資料sql語句即可。

Android中給App清除快取

具體實現 public class datacleanmanager return getformatsize cachesize 清除快取 param context public static void clearallcache context context deletedir storag...

清除快取功能

public class cachedatamanager return getformatsize cachesize public static void clearallcache context context private static boolean deletedir file di...

Android獲取快取大小和清除快取功能

拿去可以直接用 一 清除資料都有哪些 1 file 普通檔案儲存,對應路徑 data data com.files 應用內檔案 得到的方法getfiledir 2 database 資料庫檔案 db檔案 對應路徑 data data com.database 應用資料庫 3 shareprefere...