iOS離線數據緩存讀取釋放

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

1.保存到沙盒的代碼
+ (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];  
 }
讀取之前首先根據type和Id判斷本地是否有  
+ (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;  
}  
如果沙盒里面有數據
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;  

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

 1、設置全局的Cache
在AppDelegate.h中添加一個全局變量
@interface AppDelegate : UIResponder   
 {  
ASIDownloadCache *myCache;  
}  
@property (strong, nonatomic) UIWindow *window;  
@property (nonatomic,retain) ASIDownloadCache *myCache;   
在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代碼
//自定義緩存  
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方法中添加如下語句
[myCache release];  

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

2、設置緩存策略
在實現ASIHTTPRequest請求的地方設置request的存儲方式,代碼如下
 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、清理緩存數據

我在這里采用的是手動清理數據的方式,在適當的地方添加如下代碼,我將清理緩存放在了應用的設置模塊:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  
[appDelegate.myCache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 本人ios初學者,為自己學習方便,復制各位大神的學習性文章放在自己簡書里,僅作為自己學習方便使用,如果作者疑此行為...
    bu再等閱讀 2,502評論 1 1
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結起來就是把...
    Dove_iOS閱讀 27,217評論 30 472
  • 序言 目前形勢,參加到iOS隊伍的人是越來越多,甚至已經到供過于求了。今年,找過工作人可能會更深刻地體會到今年的就...
    麥兜兜買兜兜閱讀 692評論 1 4
  • 來自網絡 序言 目前形勢,參加到iOS隊伍的人是越來越多,甚至已經到供過于求了。今年,找過工作人可能會更深刻地體會...
    用心在飛閱讀 847評論 5 4
  • 序言目 前形勢,參加到iOS隊伍的人是越來越多,甚至已經到供過于求了。今年,找過工作人可能會更深刻地體會到今年的就...
    階梯閱讀 481評論 0 1