本人ios初學(xué)者癌刽,為自己學(xué)習(xí)方便,復(fù)制各位大神的學(xué)習(xí)性文章放在自己簡(jiǎn)書里尝丐,僅作為自己學(xué)習(xí)方便使用显拜,如果作者疑此行為侵權(quán),請(qǐng)隨時(shí)聯(lián)系本人刪除爹袁,如有共同學(xué)習(xí)者復(fù)制此文章远荠,請(qǐng)注明原出處
為了節(jié)約流量,同時(shí)也是為了更好的用戶體驗(yàn)失息,目前很多應(yīng)用都使用本地緩存機(jī)制譬淳,其中以網(wǎng)易新聞的緩存功能最為出色档址。我自己的應(yīng)用也想加入本地緩存的功能,于是我從網(wǎng)上查閱了相關(guān)的資料邻梆,發(fā)現(xiàn)總體上說(shuō)有兩種方法守伸。一種是自己寫緩存的處理,一種是采用ASIHTTPRequest中的ASIDownloadCache浦妄。
方法一:一般將服務(wù)器第一次返回的數(shù)據(jù)保存在沙盒里面含友。這樣在手機(jī)斷網(wǎng)的情況下可以從本地讀取數(shù)據(jù)了。
1.保存到沙盒的代碼:
+(void)saveCache:(int)typeandID:(int)_idandString:(NSString*)str;
{
NSUserDefaults*setting=[NSUserDefaultsstandardUserDefaults];
NSString*key=[NSStringstringWithFormat:@"detail-%d-%d",type,_id];
[settingsetObject:strforKey:key];
[settingsynchronize];
}
2.讀取本地沙盒的代碼
讀取之前首先根據(jù)type和Id判斷本地是否有
+(NSString*)getCache:(int)typeandID:(int)_id
{
NSUserDefaults*settings=[NSUserDefaultsstandardUserDefaults];
NSString*key=[NSStringstringWithFormat:@"detail-%d-%d",type,_id];
NSString*value=[settingsobjectForKey:key];
returnvalue;
}
如果沙盒里面有數(shù)據(jù)
NSString*value=[ToolgetCache:5andID:self.QiuTime];
if(value){
NSDictionary*backdict=[valueJSONValue];
if([backdictobjectForKey:@"items"]){
NSArray*array=[NSArrayarrayWithArray:[backdictobjectForKey:@"items"]];
for(NSDictionary*qiushiinarray){
QiuShi*qs=[[[QiuShialloc]initWithDictionary:qiushi]autorelease];
[self.listaddObject:qs];
}
}
[self.tableViewreloadData];
}
[self.tableViewtableViewDidFinishedLoadingWithMessage:@"數(shù)據(jù)全部加載完了.."];
self.tableView.reachedTheEnd=YES;
方法二:使用ASIHTTPRequest和ASIDownloadCache實(shí)現(xiàn)本地緩存
1校辩、設(shè)置全局的Cache
在AppDelegate.h中添加一個(gè)全局變量
@interfaceAppDelegate:UIResponder
{
ASIDownloadCache*myCache;
}
@property(strong,nonatomic)UIWindow*window;
@property(nonatomic,retain)ASIDownloadCache*myCache;
在AppDelegate.m中的-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions方法中添加如下代碼
//自定義緩存
ASIDownloadCache*cache=[[ASIDownloadCachealloc]init];
self.myCache=cache;
[cacherelease];
//設(shè)置緩存路徑
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*documentDirectory=[pathsobjectAtIndex:0];
[self.myCachesetStoragePath:[documentDirectorystringByAppendingPathComponent:@"resource"]];
[self.myCachesetDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
在AppDelegate.m中的dealloc方法中添加如下語(yǔ)句
[myCacherelease];
到這里為止窘问,就完成了全局變量的聲明。
2宜咒、設(shè)置緩存策略
在實(shí)現(xiàn)ASIHTTPRequest請(qǐng)求的地方設(shè)置request的存儲(chǔ)方式惠赫,代碼如下
NSString*str=@"http://....../getPictureNews.aspx";
NSURL*url=[NSURLURLWithString:str];
ASIHTTPRequest*request=[ASIHTTPRequestrequestWithURL:url];
//獲取全局變量
AppDelegate*appDelegate=[[UIApplicationsharedApplication]delegate];
//設(shè)置緩存方式
[requestsetDownloadCache:appDelegate.myCache];
//設(shè)置緩存數(shù)據(jù)存儲(chǔ)策略,這里采取的是如果無(wú)更新或無(wú)法聯(lián)網(wǎng)就讀取緩存數(shù)據(jù)
[requestsetCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
request.delegate=self;
[requeststartAsynchronous];
3故黑、清理緩存數(shù)據(jù)
我在這里采用的是手動(dòng)清理數(shù)據(jù)的方式儿咱,在適當(dāng)?shù)牡胤教砑尤缦麓a,我將清理緩存放在了應(yīng)用的設(shè)置模塊:
AppDelegate*appDelegate=[[UIApplicationsharedApplication]delegate];
[appDelegate.myCacheclearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
這里清理的是ASICachePermanentlyCacheStoragePolicy這種存儲(chǔ)策略的緩存數(shù)據(jù)场晶,如果更換其他的參數(shù)的話混埠,即可清理對(duì)應(yīng)存儲(chǔ)策略的緩存數(shù)據(jù)。
(參考:http://zycto.blog.163.com/blog/static/17152400201110221340738/)