AlamoFire忽略缓存控制标头 [英] AlamoFire Ignore Cache-Control Headers
问题描述
使用AlamoFire执行请求/处理响应时,是否可以忽略 cache-control
标头?
Is it possible to ignore cache-control
headers when performing a request/handling the response with AlamoFire?
当前,我正在按以下方式进行请求,并且服务器正在返回大型 cache-control
标头,而实际上我们需要忽略它们。
Currently I am making a request as follows, and the server is returning large cache-control
headers, when in fact we need to ignore them.
Alamofire.request(.GET, url).responseJSON { (_, _, result) in // Do something
我知道正确的解决方案是修改服务器响应,但目前尚不可行。
I know the correct solution is to modify the server response, but this is not feasible at this time.
此外,在其他请求中,我确实希望兑现 cache-control
标头,因此理想情况下,我会提供一个解决方案
Also, there are other requests where I do want to honor the cache-control
headers, so ideally I would a solution that does not involve changing a global configuration of AlamoFire.
推荐答案
要忽略缓存的数据,您需要设置<$ c在使用Alamofire启动它之前,先在 NSURLRequest
上使用$ c> cachePolicy 。
To ignore the cached data, you need to set the cachePolicy
on the NSURLRequest
before using Alamofire to start it.
let URL = NSURL(string: "https://my_url_path...")!
let URLRequest = NSMutableURLRequest(URL: URL)
URLRequest.cachePolicy = .ReloadIgnoringCacheData
Alamofire.request(URLRequest)
.response { response in
print(response)
}
设置<$ c URL请求上的$ c> cachePolicy 始终会覆盖
NSURLSessionConfiguration
上设置的值。
Setting the
cachePolicy
on the URL request always overrides the value set on theNSURLSessionConfiguration
.
默认情况下,NSURLSessionConfiguration缓存策略设置为 .UseProtocolCachePolicy
,它将遵守 Cache-Control
标头值。
By default, the NSURLSessionConfiguration cache policy is set to .UseProtocolCachePolicy
which will honor the Cache-Control
header values.
这篇关于AlamoFire忽略缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!