Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Alamofire请求中检查来自缓存或服务器的网络响应?_Ios_Swift_Alamofire - Fatal编程技术网

Ios 如何在Alamofire请求中检查来自缓存或服务器的网络响应?

Ios 如何在Alamofire请求中检查来自缓存或服务器的网络响应?,ios,swift,alamofire,Ios,Swift,Alamofire,我正在使用Alamofire进行网络呼叫,并在iOS应用程序中缓存响应 响应是缓存,但我无法检查收到的响应是来自缓存还是来自服务器 如何检查相同的内容?您可以检查是否有任何请求的缓存数据可用或未使用 if let cacheData = URLCache.shared.cachedResponse(for: request) { // Already cached } 你能展示你的代码让我告诉你吗?在dataTaskWillCacheResponseWithCompletion中,我正在

我正在使用Alamofire进行网络呼叫,并在iOS应用程序中缓存响应

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


如何检查相同的内容?

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

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

  // Already cached
}

你能展示你的代码让我告诉你吗?在dataTaskWillCacheResponseWithCompletion中,我正在设置响应头[“缓存控制”]=“max age=60”,我用同样的方法进行了检查,但总是给我-响应来自服务器,即使我点击脱机请求并从缓存获取数据。下面是代码//如果让cacheData=URLCache.shared.cachedResponse(for:(request.request?.urlRequest!)!){print(“数据来自缓存”)}其他{打印(“数据来自服务器”)}