Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?
caching, cocoa-touch
Solution
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;
}
Problem
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!