views:

2755

answers:

5

Hiya,

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!

Any help here would be much appreciated.

Thanks! Nick.

+2  A: 

If you use NSURLConnection take a look at the delegate:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

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.

catlan
Thanks so much here. The memory footprint of my app has suddenly halved! Nick.
Nick Cartwright
+5  A: 

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;
}
tcurdt
Thanks so much here for your help!Nick.
Nick Cartwright
Thanks for that idea - I was calling a SOAP web service like this repeatedly and it was growing the heap uncontrollably even though leaks didn't show anything was wrong. I optimized for days and finally tried to prevent caching since a lot of CFURL* objects from the internal framework were hanging around. Returning nil from willCacheResponse was the only thing that worked!
stinkbutt
+1  A: 

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:

- (void) eraseCredentials{
NSURLCredentialStorage *credentialsStorage = [NSURLCredentialStorage sharedCredentialStorage];
NSDictionary *allCredentials = [credentialsStorage allCredentials];

//iterate through all credentials to find the twitter host
for (NSURLProtectionSpace *protectionSpace in allCredentials)
 if ([[protectionSpace host] isEqualToString:@"twitter.com"]){
        //to get the twitter's credentials
  NSDictionary *credentials = [credentialsStorage credentialsForProtectionSpace:protectionSpace];
        //iterate through twitter's credentials, and erase them all
  for (NSString *credentialKey in credentials)
   [credentialsStorage removeCredential:[credentials objectForKey:credentialKey] forProtectionSpace:protectionSpace];
 }
}

I hope it works for somebody :)

calitb
A: 

So does calling a url this way cache the response on the iPhone ? I understand the need to remove the cache from memory but does it save the data from the request in some other location?

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://SomeSiteName.com/mobile/iphonelogin.aspx"]]];

A: 
  • (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

    how i use this ..where i use it please give detail description ...i am new to iphone..

    what is the purpose of this function..

muthiah
Hiya - you should probably ask your own question for this!
Nick Cartwright