[英]how to ignore cached response using Alamofire?
Using Alamofire with configurations like this, leading to error -999 request cancelled将 Alamofire 与这样的配置一起使用,导致错误 -999 请求被取消
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 20
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
var sessionManager = Alamofire.SessionManager(configuration: configuration)
sessionManager.request(url,
method: methodType,
parameters: resource.parameters,
headers: resource.headers)
However when i use it like this, directly it works fine, but it caches the responses ..但是,当我像这样使用它时,它直接工作正常,但它会缓存响应..
Alamofire.request(url,
method: methodType,
parameters: resource.parameters,
headers: resource.headers)
I need to ignore the cached data, and using the first is not working .我需要忽略缓存的数据,使用第一个不起作用。
Not sure why you are getting this error, but there are a number of ways to solve this error.不确定为什么会出现此错误,但有多种方法可以解决此错误。
1.You can add timestamp to your API url so so that you would never get a cached response. 1.您可以在您的 API url 中添加时间戳,这样您就永远不会得到缓存响应。
extension Date {
func toMillis() -> Int64! {
return Int64(self.timeIntervalSince1970 * 1000)
}
}
let currentTimeStamp = Date().toMillis()!
let url = "https://example.com?timestamp=\(currentTimeStamp)"
You can do something like:您可以执行以下操作:
var req = URLRequest(url: URL(string: "<URL>")!) req.httpMethod = "GET" req.setValue("application/json", forHTTPHeaderField: "Content-Type") req.setValue("<Auth KEY>", forHTTPHeaderField:"Authorization" ) req.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData Alamofire.request(req).validate().responseJSON { response in ...
There are many ways to accomplish this (examples use Alamofire 5):有很多方法可以实现这一点(示例使用 Alamofire 5):
Use an ephemeral
URLSessionConfiguration
:使用ephemeral
URLSessionConfiguration
:
let configuration = URLSessionConfiguration.ephemeral
configuration.headers = .default
let session = Session(configuration: configuration)
Use a URLSessionConfiguration
without a URLCache
instance:使用URLSessionConfiguration
没有一个URLCache
的实例:
let configuration = URLSessionConfiguration.af.default
configuration.urlCache = nil
let session = Session(configuration: configuration)
Set an issued URLRequest
's cachePolicy
to reloadIgnoringLocalCacheData
, which should prevent the storing of its response:将发出的URLRequest
的cachePolicy
为reloadIgnoringLocalCacheData
,这应该会阻止其响应的存储:
var request = URLRequest(...)
request.cachePolicy = .reloadIgnoringLocalCacheData
session.request(request).response...
Use Alamofire 5's CachedResponseHandler
API to prevent caching on a per request basis:使用 Alamofire 5 的CachedResponseHandler
API 来防止基于每个请求进行缓存:
session.request(...).cacheResponse(using: ResponseCacher.doNotCache).response...
-> Did you try removing cacheResponse before requesting again? -> 您是否尝试在再次请求之前删除 cacheResponse? https://stackoverflow.com/a/47869125/9298652 https://stackoverflow.com/a/47869125/9298652
-> Or try setting NSURLConnectionDelegate's willCacheResponse method to nil to prevent responses from caching. -> 或者尝试将 NSURLConnectionDelegate 的 willCacheResponse 方法设置为 nil 以防止缓存响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.