-1

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 .

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • Possible duplicate of [How to disable caching in Alamofire](https://stackoverflow.com/questions/32199494/how-to-disable-caching-in-alamofire) – Rishab Sep 03 '19 at 10:55
  • @Rishab his solution is not working for me . – Mohmmad S Sep 03 '19 at 10:57
  • I don't think caching is your real problem here. I think you should look into why your first code is returning -999 error. What happens if you remove `timeoutIntervalForRequest` and `requestCachePolicy`? – Rishab Sep 03 '19 at 11:14

3 Answers3

1

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

Varun Malhotra
  • 1,202
  • 3
  • 15
  • 28
1

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...
Jon Shier
  • 12,200
  • 3
  • 35
  • 37
0

-> 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.

Amyth
  • 383
  • 3
  • 9