Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?

On iPhone, I perform a HTTP request using NSURLRequest for a chunk of data. Object allocation spikes and I assign the data accordingly. When I finish with the data, I free it up accordingly - however instruments doesn't show any data to have been freed!

My theory is that by default HTTP requests are cached, however - I don't want my iPhone app to cache this data.

Is there a way to clear this cache after a request or prevent any data from being cached in the first place?

I've tried using all the cache policies documented a little like below:

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; theRequest.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; 

but nothing seems to free up the memory!

like image 836
Nick Cartwright Avatar asked Jan 01 '09 16:01

Nick Cartwright


People also ask

How is cached data stored?

The data in a cache is generally stored in fast access hardware such as RAM (Random-access memory) and may also be used in correlation with a software component. A cache's primary purpose is to increase data retrieval performance by reducing the need to access the underlying slower storage layer.


1 Answers

Usually it's easier to create the request like this

NSURLRequest *request = [NSURLRequest requestWithURL:url       cachePolicy:NSURLRequestReloadIgnoringCacheData       timeoutInterval:60.0]; 

Then create the connection

NSURLConnection *conn = [NSURLConnection connectionWithRequest:request        delegate:self]; 

and implement the connection:willCacheResponse: method on the delegate. Just returning nil should do it.

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {   return nil; } 
like image 179
tcurdt Avatar answered Sep 20 '22 01:09

tcurdt