NSURLRequest: Always getting NSCachedURLResponse back.

704 views Asked by At

I'm using AfNetworking, I make a call for new data but I keep getting the cached result back. So if I'm in a VC that's shows the data, pop back to the root and change data on my server, I then wait 30sec and when I push back into the VC I'll see the the old data. If I hit the URL in a browser I see the correct data. If I re-run the app I will see the changes to the data.

My response from my server sends back cache control header: Cache-Control:max-age=10, public

From what I can tell is that I always always get a NSCachedURLResponse back and that the cache is not listening to my cache-controll policy.

In my AppDelegate I set my SharedURLCache:

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:8 * 1024 * 1024 diskCapacity:8 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

How I set the URLRequest:

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

I do nothing else involving NSURLCache.

Any ideas?

BooRanger

1

There are 1 answers

5
ipmcc On

The behavior you describe is exactly what I'd expect from the code you've posted. Have you tried using NSURLRequestReloadIgnoringLocalAndRemoteCacheData instead of NSURLRequestUseProtocolCachePolicy? According to this NSURLRequestUseProtocolCachePolicy means:

Specifies that the caching logic defined in the protocol implementation, if any, is used for a particular URL load request. This is the default policy for URL load requests.

So if you want it to always load from the server, you should use NSURLRequestReloadIgnoringLocalAndRemoteCacheData which is described as:

Specifies that not only should the local cache data be ignored, but that proxies and other intermediates should be instructed to disregard their caches so far as the protocol allows.