What is the difference between URLCache.cachedResponse(for:) and URLCache.getCachedResponse(for:completionHandler:) ?

666 Views Asked by At

I am working with a server that responds with the following headers:

Cache-Control: no-cache  
last-modified: Mon, 17 Dec 2018 14:47:19 GMT  

The following code:

let myTask = URLSession.shared.dataTask(request: myRequest, completionHandler: { ... })  
myTask.resume()  

correctly sends the If-Modified-Since header.

But when I do:

URLCache.shared.cachedResponse(for: myRequest)  

I get a nil result, whereas with:

URLCache.shared.getCachedResponse(for: myTask, completionHandler: { ... })  

I get a non-nil result in the completionHandler.

I would have expected both giving me similar results. Can someone explains that to me?

1

There are 1 best solutions below

1
ZeroOne On

This checks my local cache response.

if let request = dataRequest.request {
  if (URLCache.shared.cachedResponse(for: request) != nil) {
    URLCache.shared.removeCachedResponse(for: request)
  }
}

Also you can check the definition as per apple.

enter image description here

enter image description here