简体   繁体   中英

Alamofire unable disable caching

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}

This is working:

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

And then just Alamofire.request

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.

我在每个 Alamofire 请求停止缓存之前使用URLCache.shared.removeAllCachedResponses()

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.

Alamofire 5.0 中,您应该创建一个ResponseCacher实例并将其缓存行为设置为.doNotCache ,然后将其注入新Session并仅使用该会话:

static let mySession = Session(cachedResponseHandler: ResponseCacher(behavior: .doNotCache))

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