是因?yàn)镹SURLRequest的默認(rèn)緩存機(jī)制吨岭,需將NSURLRequest的cachePolicy屬性來(lái)設(shè)置請(qǐng)求的緩存策略柱搜。
iOS對(duì)NSURLRequest提供了7種緩存策略:(實(shí)際上能用的只有4種)
NSURLRequestUseProtocolCachePolicy // 默認(rèn)的緩存策略(取決于協(xié)議)
NSURLRequestReloadIgnoringLocalCacheData // 忽略緩存棕孙,重新請(qǐng)求
NSURLRequestReloadIgnoringLocalAndRemoteCacheData // 未實(shí)現(xiàn)
NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData // 忽略緩存,重新請(qǐng)求
NSURLRequestReturnCacheDataElseLoad // 有緩存就用緩存,沒(méi)有緩存就重新請(qǐng)求
NSURLRequestReturnCacheDataDontLoad // 有緩存就用緩存九杂,沒(méi)有緩存就不發(fā)請(qǐng)求施禾,當(dāng)做請(qǐng)求出錯(cuò)處理(用于離線模式)
NSURLRequestReloadRevalidatingCacheData // 未實(shí)現(xiàn)
AFN中找到AFURLRequestSerialization.m文件 找到
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
error:(NSError *__autoreleasing *)error
加上
[mutableRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
最終代碼
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
error:(NSError *__autoreleasing *)error
{
NSParameterAssert(method);
NSParameterAssert(URLString);
NSURL *url = [NSURL URLWithString:URLString];
NSParameterAssert(url);
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
mutableRequest.HTTPMethod = method;
[mutableRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
if ([self.mutableObservedChangedKeyPaths containsObject:keyPath]) {
[mutableRequest setValue:[self valueForKeyPath:keyPath] forKey:keyPath];
}
}
mutableRequest = [[self requestBySerializingRequest:mutableRequest withParameters:parameters error:error] mutableCopy];
return mutableRequest;
}