AlamoFire Ignore Cache-Control Headers

微笑、不失礼 提交于 2019-11-29 03:59:57

问题


Is it possible to ignore cache-control headers when performing a request/handling the response with AlamoFire?

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.

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.


回答1:


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)
    }

Setting the cachePolicy on the URL request always overrides the value set on the NSURLSessionConfiguration.

By default, the NSURLSessionConfiguration cache policy is set to .UseProtocolCachePolicy which will honor the Cache-Control header values.



来源:https://stackoverflow.com/questions/32893800/alamofire-ignore-cache-control-headers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!