如何在 Alamofire 中禁用缓存

编程入门 行业动态 更新时间:2024-10-26 01:21:05
本文介绍了如何在 Alamofire 中禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我使用 Alamofire 发送两次 GET 请求时,我得到相同的响应,但我期待不同的响应.我想知道是不是因为缓存,如果是,我想知道如何禁用它.

When I send a GET request twice with Alamofire I get the same response but I'm expecting a different one. I was wondering if it was because of the cache, and if so I'd like to know how to disable it.

推荐答案

swift 3, alamofire 4

swift 3, alamofire 4

我的解决方案是:

为 Alamofire 创建扩展:

creating extension for Alamofire:

extension Alamofire.SessionManager{ @discardableResult open func requestWithoutCache( _ url: URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil)// also you can add URLRequest.CachePolicy here as parameter -> DataRequest { do { var urlRequest = try URLRequest(url: url, method: method, headers: headers) urlRequest.cachePolicy = .reloadIgnoringCacheData // <<== Cache disabled let encodedURLRequest = try encoding.encode(urlRequest, with: parameters) return request(encodedURLRequest) } catch { // TODO: find a better way to handle error print(error) return request(URLRequest(url: URL(string: "example/wrong_request")!)) } } }

并使用它:

Alamofire.SessionManager.default .requestWithoutCache("google/").response { response in print("Request: (response.request)") print("Response: (response.response)") print("Error: (response.error)") }

更多推荐

如何在 Alamofire 中禁用缓存

本文发布于:2023-11-26 01:58:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   如何在   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!