NSURLSession NSURLCache is not working as expected

I see strange behavior when using NSURLCache and have exhausted all parameters, but posting here ... so here it is:

I am trying to implement NSURLCache

as a basic caching mechanism in combination with NSURLSession

. And using Charles and Simple println()

, I notice that no matter what I do, my application keeps coming back to the server and re-requests previous requests.

I have verified that my server is sending the following headers sequentially:

 Cache-Control:public, max-age=31449600
 Content-Length:74289
 Content-Type:image/jpeg
 ETag:"abcdf"
 Expires:Sun, 03 Jan 2016 20:58:01 GMT
 Last-Modified:Sat, 09 Nov 2013 08:05:53 GMT

      

But when I make the following request:

let req = NSURLRequest(URL: NSURL(string: URLString)!, cachePolicy: .ReturnCacheDataElseLoad, timeoutInterval: 15)

      

NSURLSession

willCacheResponse:

The delegate method is not called at all:

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, willCacheResponse proposedResponse: NSCachedURLResponse, completionHandler: (NSCachedURLResponse!) -> Void) {

      

However, by simply requesting NSURLRequest(URL: NSURL(string: URLString)!)

without specifying a specific one cachePolicy

, then the call willCacheResponse:

is invoked. However, the next time the same request is made, the application will return to the server ...

I am not making any modifications to the response objects in any of the NSURLSession delegate methods, and my requests are everything HTTP GET

.

What am I missing?

I read a lot about this on NSURLCache:

Customization: Xcode6.1, Swift, iOS8 +

Edit: I pushed the main example on github to show my issue: https://github.com/opfeffer/nsurlcache

Check it out and let me know what I am doing wrong!

+3


source to share


1 answer


I see that your GET request has parameters and for that I think we are facing the same situation.

I reached a solution overriding NSURLCache doing some customization and accessing the underlying DB, you can see it here:



https://github.com/JCardenete/NTURLCache/tree/master/NTURLCache

NSURLCache - disk caching for GET request with broken parameters

0


source







All Articles