Alamofire无法禁用缓存。

3

我无法让Alamofire或iOS停止缓存:

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

或者
URLCache.shared.removeAllCachedResponses()

我需要禁用所有请求吗?
也尝试过:
let configuration = URLSessionConfiguration.default
configuration.urlCache = nil
let manager = Alamofire.SessionManager(configuration: configuration)

这会导致出现以下错误:

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}
5个回答

4

这是有效的:

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

然后只需要使用 Alamofire.request 即可。


1
要禁用URL缓存,您需要创建自定义的Alamofire Manager,并使用nil urlCache。
let configuration = URLSessionConfiguration.default
configuration.urlCache = nil
let manager = Manager(configuration: configuration)

您可以在苹果文档中找到更多信息。

要禁用缓存,请将此属性设置为nil。


经理 == SessionManager吗?谢谢;设置SessionManager不起作用。 - Chris G.

1
在 Alamofire 5.0 中,您应该创建一个 ResponseCacher 实例,并将其缓存行为设置为 .doNotCache,然后将其注入到一个新的 Session 中,并仅使用该会话。
static let mySession = Session(cachedResponseHandler: ResponseCacher(behavior: .doNotCache))

0

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


FYI,它不起作用,Alamofire正在使用自己的机制。 - OhadM

0

您不能更改已用于初始化URLSessionURLSessionConfiguration的属性,这就是您的代码示例正在做的事情。就像k8mil所说的那样,如果您想要这种行为,应该创建自己的Alamofire SessionManager并禁用缓存。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接