cachedResponseForRequest not called

I have subclassed NSURLCache trying to see how caching works. In my app delegate, have this

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                                            diskCapacity:50 * 1024 * 1024
                                                                diskPath:nil];

[NSURLCache setSharedURLCache:sharedCache];


My requests are setup like so

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:someUrl
                                                           cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                       timeoutInterval:10];


But in my NSURLCache subclass

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request


never gets called. However

- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse
                 forRequest:(NSURLRequest *)request


Does get called.


The headers for my request responses look like

"Cache-Control" = "private, max-age=86224084";

Connection = "keep-alive";

"Content-Length" = 36178;

"Content-Type" = "image/gif";

Date = "Mon, 27 Jul 2015 12:57:35 GMT";

Server = "Microsoft-IIS/7.5";

"X-AspNet-Version" = "4.0.30319";

"X-Powered-By" = "ASP.NET";


Any idea why "cachedResponseForRequest" is not getting called?


Thanks!

Are you running your requests with NSURLConnection or NSURLSession? If it's the latter, you should look at the three 'forDataTask' methods in the NSURLSessionTaskAdditions category.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo,


I'm having exactly the same problem. I want to load a NSURLRequest in a WKWebview, the response is cached but *sometimes*

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request

is not called.


Thanks

Sven

Hi Eskimo

getCachedResponseForDataTask: completionHandler: method in NSURLSessionTaskAdditions category still not get called.

cachedResponseForRequest not called
 
 
Q