NSURLRequestReturnCacheDataElseLoad not working? - ios

I've used NSURLRequestReturnCacheDataElseLoad but it does not work, ie, it always loads data from server as every time the data I received is different.
I'm wondering that:
even with NSURLRequestReturnCacheDataElseLoad policy, it also obeys the cache control headers from server's response, regardless of document saying ignoring expire date?
What is the storage policy for [NSURLCache sharedURLCache]? If it is in memory only, then next time I start the app it won't have cache on disk?
I found this very interesting:
NSURLRequestReturnCacheDataElseLoad not loading from cache on first request?
which says
it seems this problem only exists when there's a query in the url.
Is that a confirmed problem?
Thanks

Well this topic is almost 5 years old, but I just recently had the same problem. In my case, I was using a NSURLSessionDownloadTask which, according to Can I use HTTP caching with an NSURLSessionDownloadTask on iOS?, doesn't use caching no matter which caching policy is used. I switched my code to use NSURLSessionDataTask and the NSURLRequestReturnCacheDataElseLoad policy worked as expected.

Related

iOS: Are there any issues that can occur when setting cache policy to no?

I don't want my App storing any URL data in the cache.db file. I took advice on StackOverflow and set the cache URL policy to NSURLRequestReloadIgnoringLocalCacheData like this.
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
However, I'm not sure if there are any issues that can arise from doing this. Any tips or suggestions are appreciated.
Edit: I haven't ran into any issues regarding requests or the App in general, but I like to make sure.
There won't be any issues with this, however you have to ensure, that the named behaviour is really the one you want. E.g. you have a caching mechanism implemented somewhere else.
This policy does exactly what is supposed to do. It ignores any local cache data and loads any data directly from a remote source. However, you're not protected from any intermediate caching mechanisms.
You can include proper headers to your request to omit any intermediate caching (Cache-Control: no-cache to tell some proxies what do you want to do with the request for example)

NSURLRequest has serious problems in iOS

I am unable to delete the cache for a request, on top of that I can't use NSURLRequestCachePolicy to Ignore local and remote cache data. What to do?
It seems impossible to just ignore the cache.
Some things that I have tried but they didn't work:
Removing the cache for a specific request
Removing all cache
Creating a request with a ReloadIgnoringLocalAndRemoteCacheData policy
Do you have any suggestions?
The thing that we can't get rid of the cache reminds me of the quote: "Once you go black you never go back".
Answering my own question: Resetting the NSURLSession seems to work, I did that along with having a request with a ReloadIgnoringLocalAndRemoteCacheData policy, I don't know if it will work without that, but in this combination they seem to work.
I have reseted the URLSession with this code: with a ReloadIgnoringLocalAndRemoteCacheData policy

Get cached Data of webpage in my iOS app

I am using NSUrlConnection to grab some data from a web service that returns JSON data when I hit a url but sometimes the page goes down or something is wrong.
Is there a way to use the cached version of the url in my iOS App to grab that data when the webpage returns a 404?
In the browser if I go to cache:mysite.com/test.php I can see the JSON data even when the page is down but when I try to use the same url in my iOS App, I do not get the JSON data back.
Is there maybe an Obj C class I am not aware of or an option for the NSUrlConnection?
NSURLConnection already uses a cache. By default it only caches responses in-memory, you can customize this by setting it to also use on-disk storage:
NSURLCache *result = [[NSURLCache alloc] initWithMemoryCapacity:[(1024*1024*512) diskCapacity:(1024*1024*1024 * 100) diskPath#"Cache.db"]:
[NSURLCache setSharedURLCache:result];
The reason you are seeing the behavior you describe in your question is that the remote web service is telling your client not to cache the response. You can check this using REDbot or a tool like Charles. By default NSURLRequest will use the protocol's caching policy and semantics, which is almost always the correct thing to do. You can specify a different cache policy by changing the cachePolicy property of the NSURLRequest.
After much search, I didn't find a way to get a cached version of a web page in my iOS app but I handled the 404 error by looking for the response status code and using a service like Google Cache or archive.org/web/ to get the cached version of the url if a 404 was found.
Might not be the most delicate way but I could not find another way to get a cached version of the website when making the request.

How does NSURLRequestReturnCacheDataElseLoad work in iOS?

In my application I am using Webview and loading a data which I am getting from the webserver. In this it's working fine but issue is I used NSURLRequest object for sending request to the server. And I wrote the code as such below
NSMutableURLRequest * webPageRequest=[NSMutableURLRequest requestWithURL:webPageUrl cachePolicy:2 timeoutInterval:60.0];
Here the data is caching in memory successfully. But issue is when ever I open app it is always giving old data. It's not sending a request to the server for new data and even the app data is also not updating. I am supporting both IOS6 & IOS7.
So can you please let me know how to resolve this issue. And may I know how long the data will be available in Cache memory. And how NSURLRequestReturnCacheDataElseLoad will work means at what situation this method will execute.
The issue you can see the old data is because the NSURLRequestReturnCacheDataElseLoad, see this (it has been taken from NSHister:
NSURLRequestReturnCacheDataElseLoad: Existing cached data should be
used, regardless of its age or expiration date. If there is no
existing data in the cache corresponding to the request, the data is
loaded from the originating source.
Check out this link you should find the way to use right policy for your purpose.

AFNetworking and caching

I am downloading WMS tiles which I want to cache. I'm using AFNetworking which includes NSURLCache. The responses from the server do not contain Cache-Control protocols in the header.
I asked the server guy about this and was unfamiliar with server side cache-control. At the moment, he is swamped with other work. Do I need him to implement the cache-control or can I force NSURLCache to cache them w/out the info the response header?
Is NSURLCache persistent? If so, how can I clear the cache? The tiles will need to be retrieved per session and can not be persistent.
Or should I create my own cache?
When you activate NSURLCache it will work for any request that is based on the NSUrlRequest (like AFNetworking). The moment you activate the NSURLCache you can specify it's maximum size. You can also clear the cache by calling the removeAllCachedResponses or removeCachedResponsesForRequest methods. If the server does not send any cache control information, then the cache will still cache the file. If you want complete control over the cache, you could create your own cache. If you would like to see a sample code for that, then have a look at https://github.com/evermeer/EVURLCache

Resources