3

I can not get Alamofire or iOS to stop caching:

Alamofire.SessionManager.default.session.configuration.requestCachePolicy = .reloadIgnoringLocalCacheData

or

URLCache.shared.removeAllCachedResponses()

I need to disable it for all requests?

Also tried:

let configuration = URLSessionConfiguration.default
configuration.urlCache = nil
let manager = Alamofire.SessionManager(configuration: configuration)

This give this error:

Auth request failed with error:
 Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=http://localhost:8080/slow/file.json, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http://localhost:8080/slow/file.json}
Nishant Bhindi
  • 2,242
  • 8
  • 21
Chris G.
  • 23,930
  • 48
  • 177
  • 302

5 Answers5

4

This is working:

URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)

And then just Alamofire.request

Chris G.
  • 23,930
  • 48
  • 177
  • 302
1

To disable you urlCache you have to create custom Alamofire Manager with nil urlCache.

let configuration = URLSessionConfiguration.default
configuration.urlCache = nil
let manager = Manager(configuration: configuration)

More information you can find in Apple Documenation

To disable caching, set this property to nil.

kamwysoc
  • 6,709
  • 2
  • 34
  • 48
1

In Alamofire 5.0 you should create an instance of ResponseCacher and set its caching behavior to .doNotCache and then inject it to a new Session and use only that session:

static let mySession = Session(cachedResponseHandler: ResponseCacher(behavior: .doNotCache))
OhadM
  • 4,687
  • 1
  • 47
  • 57
0

I used URLCache.shared.removeAllCachedResponses() before each Alamofire request to stop caching

BadCodeDeveloper
  • 455
  • 5
  • 24
0

You cannot alter the properties of a URLSessionConfiguration that has already been used to initialize a URLSession, which is what your code sample is doing. Like k8mil said, you should create your own Alamofire SessionManager with the cache disabled if you want this behavior.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37