NSURLCache not being used - ios

I am attempting to use NSURLCache so that my app will save JSON responses from a web server and not request it so much.
I have added Cache-Control:Max-Age=604800 to the response headers in the request.
I have added the following code my apps AppDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//set up the URL cache
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
}
And my request in the code looks like this:
NSString *myURLString = [NSString stringWithFormat:#"http://domain.com/api/?location=%#,%#&date=%#&method=%#", latitude, longitude, curDate, methodString];
NSLog(#"%#", myURLString);
NSURL *url = [NSURL URLWithString:myURLString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
I do not think this is working, because when running with instruments network profile I see a spike in network request each time the view is loaded and if I load the app then put the phone in plane mode no data loads (whereas I suspect it would use the cache).
Is this proof that no cache is being made/used? And, if so, does anyone have any ideas why not? And, if it's not proof, does anyone know how I can better test the caching?
Many thanks.

+[NSData dataWithContentsOfURL:] makes absolutely no guarantees about what caching model it will follow. In practice at present, it deliberately ignores any cache.
You should instead use NSURLConnection which exposes proper caching controls.

Related

How to enable disk cache in AFNetworking on iOS

in my iOS app I'm the UIImageView category provided by AFNetworking in order to download images stored on Amazon S3. I thought that the default cache stored cached images on disk by default, however, for a number of reasons (missing HTTP headers, etc.) I realized I was mistaken. Images don't seem to be cached, they are downloaded at every execution.
Therefore I'm now trying a custom cache, as suggested here http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/
Here's what I've done so far (which seems to work):
1) defined a custom NSURLCache called PlayerImageCache and initialized it in my app delegate in order to use 5MB of memory and up to 100MB of disk space
PlayerImageCache *sharedCache = [[PlayerImageCache alloc] initWithMemoryCapacity:5*1024*1024 diskCapacity:100*1024*1024 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
2) overrided the storeCachedResponse method in order to always cache my images
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse
forRequest:(NSURLRequest *)request
{
if ([request.URL.absoluteString hasPrefix:playersImagesPrefix])
{
NSCachedURLResponse *modifiedCachedResponse = [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:cachedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed];
[super storeCachedResponse:modifiedCachedResponse forRequest:request];
}
else
{
[super storeCachedResponse:cachedResponse forRequest:request];
}
}
3) forced every request to check the cache before downloading the image by using the NSURLRequestReturnCacheDataElseLoad policy
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL urlForPlayer:self.playerId] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
[self.playerImage setImageWithURLRequest:imageRequest placeholderImage:[UIImage imageNamed:#"playerprofile"] success:nil failure:nil];
Will this be enough? Do I need to further customize my cache? Will this work even without the Cache-Control HTTP header?
The answer is yes! It works like a charm. I managed to greatly decrease the number of requests thanks to this trick.

NSURLConnection is growing in instruments, is this NSURLCache?

So I have a memory problem with my app. The app has a MKMapView with MKOverlayRenderer which loads images on the map. Everything working ok but after 30-60 minutes the app crashes due memory.
With Instruments I found that NSURLConnection is growing and because I cannot find anything else (besides all kind of stuff I dont understand) I think thats is my problem.
Screenshot instruments after runnning for 1-2 minutes:
Screenshot instruments after running for 12-13 minutes:
Images are loaded like this in the canDrawMapRect method and saved to tmp folder (if I turn this off the NSURLConnection won't grow that high):
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request
queue:[OperationQueues sharedOperationQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[data writeToFile:path atomically:YES];
Then loaded in the drawMapRect method.
I tried fixing this with the following code:
AppDelegate:
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:#"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory
diskCapacity:cacheSizeDisk
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
ViewController:
- (void)didReceiveMemoryWarning
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[super didReceiveMemoryWarning];
}
right after the request: [added]
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
But as you can see in Instruments without any luck.
Could this be my problem?
Is this the NSURLCache which is growing or something else?
As requested a couple more screenshots from instruments:
First, 400 network requests in one minute seems pretty excessive. Some of those requests are probably duplicated or unnecessary. The amount of calls is obviously downloading a lot of information, and not giving the os much time to cleanup.
Second, your NSURLCache is actually providing more memory space than the default cache. I tested on my device/simulator and ios provides a default memory capacity of 512 000 bytes. You're caches are initialized with 4 194 304 bytes (and way more on disk).
I would suggest that you set your cache size smaller to see some improvement. However, investigating the amount of network requests that are made will provide longer term benefits.

NSURLCache does not clear stored responses in iOS8

Here is the sample function I call when i need to clear cache and make a new call to URL
- (void)clearDataFromNSURLCache:(NSString *)urlString
{
NSURL *requestUrl = [NSURL URLWithString:urlString];
NSURLRequest *dataUrlRequest = [NSURLRequest requestWithURL: requestUrl];
NSURLCache * cache =[NSURLCache sharedURLCache];
NSCachedURLResponse* cacheResponse =[cache cachedResponseForRequest:dataUrlRequest];
if (cacheResponse) {
NSString* dataStr = [NSString stringWithUTF8String:[[cacheResponse data] bytes]];
NSLog(#"data str r= %#",dataStr);
NSLog(#"url str r= %#",[[[cacheResponse response] URL] absoluteString]);
[cache storeCachedResponse:nil forRequest:dataUrlRequest];
[NSURLCache setSharedURLCache:cache];
}
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:dataUrlRequest];
//Check if the response data has been removed/deleted from cache
NSURLRequest *finalRequestUrlRequest = [NSURLRequest requestWithURL:requestUrl];
NSURLCache * finalCache =[NSURLCache sharedURLCache];
NSCachedURLResponse* finalcacheResponse =[finalCache cachedResponseForRequest:finalRequestUrlRequest];
if (finalcacheResponse) {
//Should not enter here
NSString* finaldataStr = [NSString stringWithUTF8String:[[finalcacheResponse data] bytes]];
NSLog(#"data str r= %#",finaldataStr);
NSLog(#"url str r= %#",[[[cacheResponse response] URL] absoluteString]);
}
}
In iOS 6/7 the response is deleted successfully for the requestURL, but in iOS 8 it never gets deleted.
I have searched but could not find any reason why this should not work in iOS8.
Any help will be appreciated…..
NSURLCache is broken on iOS 8.0.x - it never purges the cache at all, so it grows without limit. See http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/ for a detailed investigation. Cache purging is fixed in the 8.1 betas - but removeCachedResponseForRequest: is not.
removeCachedResponsesSinceDate: does appear to work on iOS 8.0 - an API that was added for 8.0, but hasn't made it to the docs yet (it is in the API diffs). I am unclear what use it is to anyone - surely what you normally want to do is remove cached responses before a particular date.
removeAllCachedResponses works as well - but that's a real sledgehammer solution.
I got a sufficient result reseting the cached response for a specific URL, changing the cache control to something that would never be returned like a "max-age=0" in the header. Look here
Whenever any API call is generated, That response is saved in the cache. You are able to find that folder in your documents directory, a folder with named cachedb is created which contains a list of responses. It can be a major concern related to security. With any third-party tool, someone can have access to that information.
Below is the way I have solved this issue :
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:0 * 1024 * 1024 diskCapacity:0 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:cache];
I have allocated 0 memory space to the cache. so no data will be stored in Cache memory.

NSURLCache not working after iOS app termination

I am building an app in which I am implementing offline mode as well. For this I used NSURLCache mechanism. When the app is in foreground then the cache mechanism works perfectly even if the device goes in offline mode. The problem comes when I quit the app and then open it in offline mode. This time NSCachedURLResponse is returning nil for the particular request.
Below is the code I am using for this:
Custom Cache Creation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024 diskCapacity:1024*1024*5 diskPath:nil];
[NSURLCache setSharedURLCache:urlCache];
}
Calling server and cached object:
-(void)serverCall
{
NSURLCache *urlCache = [NSURLCache sharedURLCache];
NSString *urlString = nil;
NSHTTPURLResponse *response = nil;
NSError *error = nil;
urlString = [NSString stringWithFormat:#"%#%#",BASE_URL,url];
urlString = [urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *finalUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
NSData *data = nil;
if ([self checkInternetConnection])
{
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
[urlCache storeCachedResponse:cachedResponse forRequest:request];
}
else
{
NSCachedURLResponse *cachedResponse = [urlCache cachedResponseForRequest:request];
response = cachedResponse.response;
data = cachedResponse.data;
}
}
I can see that caches.db in library path of application the response is saved. But when i am trying to read it in Offline mode (after app is killed from background), the cached response is coming nil. I have gone through following links (and many more) but couldn't find the solution of my problem.
NSURLCache Problem with cache response
How to use NSURLCache to return cached API responses when offline (iOS App)
http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/
http://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/
Bypassing http response header Cache-Control: how to set cache expiration?
I have checked the http header fields and cached header in the api response and the problem is not from server end.
I have also tried providing a path in diskPath param while creating the custom cache but then it doesn't even load the cached object while the app is in foreground and internet disconnects. I have also changed the expiration date but the working is still same.
I have tried using SDURLCache but I am facing the similar problem. However I have successfully achieved this with ASIHTTPRequest but I don't want to use it as I have written all the server operations once already and I have to change every request with ASIHTTPRequest method.
Please help me find a solution for this.
Thanks in advance....

NSURLCache not caching url

I create the url like so, which is part of a function that is called in a background thread.
NSURL *url = [NSURL URLWithString:urlString];
NSData *pageData = [NSData dataWithContentsOfURL:url];
And when I try to cache it
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
}
However, if I open the app, close it, and try opening it again, the app takes the same amount of time to load and there is no noticeable improvement in speed. I'm wondering if there is an iOS limitation with caching, or if I have implemented the code incorrectly.
Are you including the proper e-tag in your request? Is your server returning a 200, or a 304?
In order to know that the cached data can be used, the server needs to indicate that it hasn't changed.

Resources