數據離線緩存與讀取

方法一:一般將服務器第一次返回的數據保存在沙盒裏面。這樣在手機斷網的情況下可以從本地讀取數據了。

1.保存到沙盒的代碼:

[plain] view plaincopy
  1. + (void)saveCache:(int)type andID:(int)_id andString:(NSString *)str;  
  2. {  
  3.     NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];  
  4.     NSString * key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];  
  5.     [setting setObject:str forKey:key];  
  6.     [setting synchronize];  
  7. }  

2.讀取本地沙盒的代碼

讀取之前首先根據type和Id判斷本地是否有

[plain] view plaincopy
  1. + (NSString *)getCache:(int)type andID:(int)_id  
  2. {  
  3.     NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];  
  4.     NSString *key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];  
  5.       
  6.     NSString *value = [settings objectForKey:key];  
  7.     return value;  
  8. }  

如果沙盒裏面有數據

[plain] view plaincopy
  1. NSString *value = [Tool getCache:5 andID:self.QiuTime];  
  2.         if (value) {  
  3.             NSDictionary *backdict = [value JSONValue];  
  4.             if ([backdict objectForKey:@"items"]) {  
  5.                 NSArray *array=[NSArray arrayWithArray:[backdict objectForKey:@"items"]];  
  6.                 for (NSDictionary *qiushi in array) {  
  7.                     QiuShi *qs=[[[QiuShi alloc]initWithDictionary:qiushi] autorelease];  
  8.                     [self.list addObject:qs];  
  9.                 }  
  10.             }  
  11.             [self.tableView reloadData];  
  12.              
  13.         }  
  14.           
  15.         [self.tableView tableViewDidFinishedLoadingWithMessage:@"數據全部加載完了.."];  
  16.         self.tableView.reachedTheEnd  = YES;  




方法二:使用ASIHTTPRequest和ASIDownloadCache實現本地緩存

1、設置全局的Cache
    在AppDelegate.h中添加一個全局變量

[plain] view plaincopy
  1. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  2. {  
  3.     ASIDownloadCache *myCache;  
  4. }  
  5. @property (strong, nonatomic) UIWindow *window;  
  6. @property (nonatomic,retain) ASIDownloadCache *myCache;  

   在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代碼

[plain] view plaincopy
  1. //自定義緩存  
  2. ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];  
  3. self.myCache = cache;  
  4. [cache release];  
  5.       
  6. //設置緩存路徑  
  7. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  8. NSString *documentDirectory = [paths objectAtIndex:0];  
  9. [self.myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:@"resource"]];  
  10. [self.myCache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];  
    

    在AppDelegate.m中的dealloc方法中添加如下語句

[plain] view plaincopy
  1. [myCache release];  

    到這裏爲止,就完成了全局變量的聲明。

    2、設置緩存策略

    在實現ASIHTTPRequest請求的地方設置request的存儲方式,代碼如下

[plain] view plaincopy
  1. NSString *str = @"http://....../getPictureNews.aspx";  
  2. NSURL *url = [NSURL URLWithString:str];  
  3. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  4. //獲取全局變量  
  5. AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  
  6. //設置緩存方式  
  7. [request setDownloadCache:appDelegate.myCache];  
  8. //設置緩存數據存儲策略,這裏採取的是如果無更新或無法聯網就讀取緩存數據  
  9. [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];  
  10. request.delegate = self;  
  11. [request startAsynchronous];  

    3、清理緩存數據

    我在這裏採用的是手動清理數據的方式,在適當的地方添加如下代碼,我將清理緩存放在了應用的設置模塊:

[plain] view plaincopy
  1. AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  
  2. [appDelegate.myCache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];  


    這裏清理的是ASICachePermanentlyCacheStoragePolicy這種存儲策略的緩存數據,如果更換其他的參數的話,即可清理對應存儲策略的緩存數據。
-----參考一:http://blog.csdn.net/kbawyg/article/details/7985513

-----參考二:http://zyc-to.blog.163.com/blog/static/17152400201110221340738/


備註:判斷手機是否有網絡
使用官方自帶的Reachability.h判斷

[plain] view plaincopy
  1. -(BOOL)isNetworkRunning;  
  2. {  
  3.     Reachability *r = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];  
  4.     switch ([r currentReachabilityStatus]) {  
  5.         case NotReachable:  
  6.             return FALSE;  
  7.             break;  
  8.         case ReachableViaWWAN:  
  9.             return TRUE;  
  10.             break;  
  11.         case ReachableViaWiFi:  
  12.             return TRUE;  
  13.             break;  
  14.     }  
  15.     return FALSE;  
  16.   
  17. }  


記得添加SystemConfiguration框架。
感謝原作 辛苦了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章