【问题标题】:How to check network response is from Cache or Server in Alamofire request?如何检查网络响应来自 Alamofire 请求中的缓存或服务器?
【发布时间】:2019-01-14 04:47:15
【问题描述】:

我在 iOS 应用中使用 Alamofire 和缓存响应进行网络调用。

响应正在缓存,但我无法检查收到的响应是来自缓存还是服务器?

如何检查?

【问题讨论】:

  • 你能把你的代码给我告诉你吗?
  • 在 dataTaskWillCacheResponseWithCompletion 我正在设置响应头["Cache-Control"] = "max-age=60"

标签: ios swift alamofire


【解决方案1】:

您可以检查是否有任何请求的缓存数据可用或未使用。

if let cacheData = URLCache.shared.cachedResponse(for: request) {

  // Already cached
}

【讨论】:

  • 我检查了相同但总是给我 - 即使我点击离线请求并从缓存中获取数据,响应也是来自服务器。下面是代码。 //它的 Alamofire 请求 if let cacheData = URLCache.shared.cachedResponse(for: (request.request?.urlRequest!)!) { print("Data is from cache") }else{ print("Data is from Server") }
猜你喜欢
  • 2012-10-19
  • 2020-09-21
  • 2016-11-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 2018-01-05
  • 2018-09-02
相关资源
最近更新 更多