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!
Would it be possible it's not cache related? Have you tried inspecting the data to see if data that should have been reloaded is in fact the old one? Maybe your leek is coming from elsewhere. How do you initialize and free the NSURLRequests? That might be of help to diagnose the problem.
FYI – If you want to remove the files by brute force, there is example code to do that here: salesforce.stackexchange.com/a/69736
If you use NSURLConnection take a look at the delegate:
Return Value
The actual cached response to store in the cache. The delegate may return cachedResponse unmodified, return a modified cached response, or return nil if no cached response should be stored for the connection.
Usually it’s easier to create the request like this
Then create the connection
and implement the connection:willCacheResponse: method on the delegate. Just returning nil should do it.
I have the same problem in my app when I requested info from twitter. In my case I didn’t need to preserve those credentials, so I simple erase them using the next code:
I hope it works for somebody 🙂
If not specific to a single request(U want disable cache for whole app) below one is the best option.Add this code in app delegate or based on ur need any where
If you’re using
NSURLSession
, another solution to prevent request and parameters being written to theCache.db
iOS creates within the app’sCaches
directory, is to set theNSURLCache
for the session’s configuration to a 0 size memory and 0 size disk cache e.g.or as mentioned above set at a global cache level
Presumably it’s the 0 for disk size that stops iOS writing to disk but if you have a policy to
reloadIgnoringLocalCacheData
then you probably aren’t interested in memory caching either.Note This will prevent any
Caches/Cache.db
(requests & responses) orCaches/fsCachedData/
folder (response data) being created at all. We’ve decided to take this approach in an app for security purposes as we don’t want our requests to be stored on disk cache ever.If anyone knows is there’s a way to stop only request caching but keep response data caching from the iOS URL Loading mechanism, I’d be interested to know. (there’s no API or official documentation about this from what I can tell)
Assuming the server is correctly implemented, putting the
Cache-Control:no-store
header in the request will generate a server response with the same header, thus causingNSURLCache
to not store the response data on disk.Therefore, no need for the shotgun approach of disabling
NSURLCache
disk caching.PS: Adding the header should work for all HTTP frameworks, like
AFNetworking