不管怎么說,都是自己接觸不多,了解有限。前些天APP上需要一個維護(hù)公告,后臺直接丟了一個json 文件鏈接 過來,好呀,分分鐘擼到代碼里,然后在后臺控制開關(guān)。好家伙,無論開還是關(guān),前端拿到的json都是第一次獲得的數(shù)據(jù),糾結(jié)了一小會兒,決定用原生的請求試試,結(jié)果一樣。
后來一想,MD 這是緩存 ??,??,知道是怎么回事就好辦了。(請求時,系統(tǒng)會自動識別是否為文件類型,如果是會自動緩存)
- 以NSURLConnection為例(AFNetworking同理)
NSURL *url = [NSURL URLWithString:URL_PUBLICNOTICE];
//所有的請求 會自己判斷是否是文件,如果是文件,就會緩存,以后讀數(shù)據(jù) 就從緩存中讀取 這里要實(shí)時性,所以不能緩存
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:3];
// 3、 發(fā)送異步請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSError *error = nil;
if (data){
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (!error) {//如果沒有錯誤
if ([dic[@"status"] isEqual:@1]) {
[weakSelf creatLedLabelWithText:dic[@"message"]];
}
}
}
}];