標籤:har 擷取 ring tpi loaddata dap win dir resource
為了節約流量,同時也是為了更好的使用者體驗,目前很多應用都使用本機快取機制,其中以網易新聞的緩衝功能最為出色。我自己的應用也想加入本機快取的功能,於是我從網上查閱了相關的資料,發現總體上說有兩種方法。一種是自己寫緩衝的處理,一種是採用ASIHTTPRequest中的ASIDownloadCache。
方法一:一般將伺服器第一次返回的資料儲存在沙箱裡面。這樣在手機斷網的情況下可以從本地讀取資料了。
1.儲存到沙箱的代碼:
[plain] view plaincopy
- + (void)saveCache:(int)type andID:(int)_id andString:(NSString *)str;
- {
- NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
- NSString * key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];
- [setting setObject:str forKey:key];
- [setting synchronize];
- }
2.讀取本地沙箱的代碼
讀取之前首先根據type和Id判斷本地是否有
[plain] view plaincopy
- + (NSString *)getCache:(int)type andID:(int)_id
- {
- NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
- NSString *key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];
-
- NSString *value = [settings objectForKey:key];
- return value;
- }
如果沙箱裡面有資料
[plain] view plaincopy
- NSString *value = [Tool getCache:5 andID:self.QiuTime];
- if (value) {
- NSDictionary *backdict = [value JSONValue];
- if ([backdict objectForKey:@"items"]) {
- NSArray *array=[NSArray arrayWithArray:[backdict objectForKey:@"items"]];
- for (NSDictionary *qiushi in array) {
- QiuShi *qs=[[[QiuShi alloc]initWithDictionary:qiushi] autorelease];
- [self.list addObject:qs];
- }
- }
- [self.tableView reloadData];
-
- }
-
- [self.tableView tableViewDidFinishedLoadingWithMessage:@"資料全部載入完了.."];
- self.tableView.reachedTheEnd = YES;
方法二:使用ASIHTTPRequest和ASIDownloadCache實現本機快取
1、設定全域的Cache
在AppDelegate.h中添加一個全域變數
[plain] view plaincopy
- @interface AppDelegate : UIResponder
- {
- ASIDownloadCache *myCache;
- }
- @property (strong, nonatomic) UIWindow *window;
- @property (nonatomic,retain) ASIDownloadCache *myCache;
在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代碼
[plain] view plaincopy
- //自訂緩衝
- ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
- self.myCache = cache;
- [cache release];
-
- //設定緩衝路徑
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentDirectory = [paths objectAtIndex:0];
- [self.myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:@"resource"]];
- [self.myCache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
在AppDelegate.m中的dealloc方法中添加如下語句
[plain] view plaincopy
- [myCache release];
到這裡為止,就完成了全域變數的聲明。
2、設定緩衝策略
在實現ASIHTTPRequest請求的地方設定request的儲存方式,代碼如下
[plain] view plaincopy
- NSString *str = @"http://....../getPictureNews.aspx";
- NSURL *url = [NSURL URLWithString:str];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- //擷取全域變數
- AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
- //設定緩衝方式
- [request setDownloadCache:appDelegate.myCache];
- //設定快取資料儲存策略,這裡採取的是如果無更新或無法連網就讀取快取資料
- [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
- request.delegate = self;
- [request startAsynchronous];
3、清理快取資料
我在這裡採用的是手動清理資料的方式,在適當的地方添加如下代碼,我將清理緩衝放在了應用的設定模組:
[plain] view plaincopy
- AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
- [appDelegate.myCache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
這裡清理的是ASICachePermanentlyCacheStoragePolicy這種儲存策略的快取資料,如果更換其他的參數的話,即可清理對應儲存策略的快取資料。(參考:http://zycto.blog.163.com/blog/static/17152400201110221340738/)
iOS開發本機快取(資料離線緩衝、讀取、釋放)