简体   繁体   中英

how to ignore cached response using Alamofire?

Using Alamofire with configurations like this, leading to error -999 request cancelled

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.

extension Date {
    func toMillis() -> Int64! {
        return Int64(self.timeIntervalSince1970 * 1000)
    }
} 

let currentTimeStamp = Date().toMillis()!
let url = "https://example.com?timestamp=\(currentTimeStamp)"
  1. 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 ...

taken from

There are many ways to accomplish this (examples use Alamofire 5):

Use an ephemeral URLSessionConfiguration :

let configuration = URLSessionConfiguration.ephemeral
configuration.headers = .default
let session = Session(configuration: configuration)

Use a URLSessionConfiguration without a URLCache instance:

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:

var request = URLRequest(...)
request.cachePolicy = .reloadIgnoringLocalCacheData
session.request(request).response...

Use Alamofire 5's CachedResponseHandler API to prevent caching on a per request basis:

session.request(...).cacheResponse(using: ResponseCacher.doNotCache).response...

-> Did you try removing cacheResponse before requesting again? https://stackoverflow.com/a/47869125/9298652

-> Or try setting NSURLConnectionDelegate's willCacheResponse method to nil to prevent responses from caching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM