I'm creating an NSURLSession
with a sessionConfiguration where I have explicitly set the requestCachePolicy
and URLCache
.
sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;
sessionConfiguration.URLCache = [NSURLCache sharedURLCache];
A request is created and passed as an argument in NSURLSession
dataTaskWithRequest:CompletionHandler:
method. The request is constructed like this:
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:kAENServiceNetworkTimeout];
Problem 1:
Setting up like this, no fsCachedData
Folder is created on disk and cached data is not used. If the request is created simply using:
request = [NSMutableURLRequest requestWithURL:url];
The cache is created on disk, and the image is used as expected. Is this a bug in Apple's code?
Problem 2:
In neither of these cases is the URLSession:dataTask:willCacheResponse:completionHandler:
delegate method called. Are their situations under which this would be the case?
Update:
I've pushed an example project to Github, https://github.com/mattmorton/cache-testing
Problem 1:
I think my understanding of the NSURLRequestReturnCacheDataElseLoad was incorrect. In this case the response is not cached.
Problem 2:
From Apple header for NSURLSession in the NSURLSessionAsynchronousConvenience category:
data task convenience methods...create tasks that bypass the normal delegate calls for response and data delivery, and provide a simple cancelable asynchronous interface to receiving data.
I am using a convenience method handler, hence why the delegate method is not being called.