[IOS]猿題庫網路json資料快取
阿新 • • 發佈:2019-02-11
使用JSON 資料快取的初衷 是提高使用者體驗,用資料快取代替loading動畫。
整個一個流程是
進入檢視控制器
載入快取—向伺服器傳送資料請求——更新資料
首先 pods 安裝猿題庫 或者從外部直接新增到工程
yrk是基於afnworking的一個封裝庫,也就是說工程裡同樣需要afn。
首先自定義一個 類
例如:
SubApi
這個介面類負責 進行的是傳送資料請求。
定義一個方法:
- (id)initWithUserId:(NSString *)userId andUsertoken:(NSString *)usertoken andTypeid:(NSString *)type;
#import "SubApi.h" @implementation SubApi { NSString *_userId; NSString *_usertoken; NSString *_typeid; NSString *_cpage; } - (id)initWithUserId:(NSString *)userId andUsertoken:(NSString *)usertoken andTypeid:(NSString *)type{ self = [super init]; if (self) { _userId = userId; _usertoken = usertoken; _type = type; } return self; } - (YTKRequestMethod)requestMethod { return YTKRequestMethodPost; } - (NSString *)requestUrl//url { return @"/URl?"; } - (id)requestArgument//引數 { return @{@"userid":_userId,@"usertoken":_usertoken,@"calltype":_typeid}; }
在使用的時候
- (void)loadCacheData { SubApi *api = [[SubApi alloc]initWithUserId:Muserid andUsertoken:token andTypeid:@"0" andMorecpage:@"0"]; //這裡講引數傳進介面並且資料請求 if ([api cacheJson])//如果資料存在 { // NSDictionary *json = [api cacheJson]; NSLog(@"—————————讀取快取—————————" ); } [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) { NSLog(@"success"); } failure:^(YTKBaseRequest *request) { NSLog(@"failed"); }]; }