3

I'm writing an iPhone application, one of it's tabs is a twitter feed, i'm parsing twitter xml and putting it nicely inside a table view. In case there is no internet connection I would like to show cache results of the last time we had internet connection and the tables were updated. I'm using NSURLCache for that like so:

NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:xmlLink]
        cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
NSURLCache *sharedCache = [NSURLCache sharedURLCache];

NSCachedURLResponse *response = [sharedCache cachedResponseForRequest:theRequest];

if (response) {
   NSLog(@"Got Response");
} else {
   NSLog(@"Didn't got Response");  
}
 self.objectFeedConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES] autorelease];

I can see that for the first time NSLog outputs "Didn't got response" since it's the first time visiting this website (http://www.twitter.com/...) I can also see that the response is being cached, since i implemented the willCacheResponse method which is being called after the initWithRequest was launched, like so:

 - (NSCachedURLResponse *) connection:(NSURLConnection *)connection 
   willCacheResponse:(NSCachedURLResponse *)cachedResponse
 {
    NSLog(@"willCache Reponse"); 
    NSCachedURLResponse *newCachedResponse = nil;    
    if ([[[[cachedResponse response] URL] scheme] isEqual:@"http"]) {
            newCachedResponse = [[[NSCachedURLResponse alloc]
            initWithResponse:[cachedResponse response]
            data:[cachedResponse data]
            userInfo:nil
            storagePolicy:[cachedResponse storagePolicy]]
            autorelease];
 }
     return newCachedResponse;
}

The second time i visit this site i can clearly see that NSLog outputs "Got Response" which means that there was a cache hit, hence willCacheResponse should not be called again, but for some strange reason it is called again and again and does not pull the information out of the cache, instead it try to cache it again.

Any idea what causing this issue ?

Thanks!

djTeller
  • 495
  • 2
  • 5
  • 14
  • I tried all kind of Policies. Basically i need to use NSURLRequestUseProtocolCachePolicy, always load from remote if there is internet connection, else load from cache, but both are giving me the same issue. – djTeller Feb 25 '10 at 23:17

3 Answers3

2

OP probably doesn't care any more, but for anyone else playing with this - You should look at the HTTP headers being returned in the response. NSURLCache will check the headers to decide if it can use the thing returned from the cache. Cache-Control: Last-Modified: Etag: etc.

Dad
  • 5,473
  • 1
  • 24
  • 32
2

Please try this library: https://github.com/rs/SDURLCache

iutinvg
  • 2,131
  • 1
  • 18
  • 18
0

If I understand your question correctly, then I had the same problem and never solved it: Does NSURLConnection take advantage of NSURLCache?

As far as I can tell, NSURLConnection just doesn't take advantage of the cache, and you have to do it yourself if you want it.

Community
  • 1
  • 1
jasoncrawford
  • 2,625
  • 2
  • 19
  • 20
  • This is exactly my problem, seems like it's only caching the response URL not it's data, so in case i want to use it i can verify that i visited a specific site already. I actually worked all day yesterday to implement the cache using NSKeyedArchive, i use the NSURLCache at the beginning just as an indication that i cached that site, if the response is no i cache it, if the response is yes i pull it out of the Archive. They should make the documentation on this subject a bit more clear, and also add some examples. – djTeller Feb 27 '10 at 14:12