NSURLCache cache response problem

I am writing an application for the iPhone, one of its tabs is the Twitter channel, I parse the xml twitter and put it well in the form of a table. If there is no Internet connection, I would like to show the cache results the last time we had an Internet connection and the tables were updated. I use NSURLCache for this like this:

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 see that for the first time, NSLog displays "Didn't get a response" since it first visited this site ( http://www.twitter.com/ .. ). I also see that the response is cached since I implemented the willCacheResponse method, which is called after initWithRequest is run, for example:

 - (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;
}

, , , NSLog "Got Response", , , willCacheResponse , - , .

, ?

!

+3
3

OP, , , , - HTTP-, . NSURLCache , , , . Cache-Control: Last-Modified: Etag: ..

+2

If I understood your question correctly, then I had the same problem and I never solved it: Can NSURLConnection use NSURLCache?

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

0
source

All Articles