Search code examples
iosnsurlcache

NSURLCache cachedResponseForRequest getting call twice


In my code i had override NSURLCache cachedResponseForRequest to send the cached response in case if downloaded already. But for some reason cachedResponseForRequest getting called twice. Could anybody know the reason?

Is this a default behaviour?


Solution

  • I'm pretty sure that the method in question is called from two different parts of the NSURLSession stack for very different reasons:

    • To get the date of the cached response, so that it can provide headers in a HEAD request asking the server if the content has been modified since then.
    • To obtain the actual data for the cached response after the server sends back a 304 (NOT MODIFIED) status code.