views:

112

answers:

1

Hi there,

I'm having trouble with caching URLResponses to disk. I want that because the data i'm downloading needs reloading when the expires-tag in it's http-header is met, not earlier. Also, I want iPod touch-Users to be able to download the data once while online and then use it offline.

i'm doing this which works fine for caching things in memory but fails when relaunching the app:

NSURLRequest* menuRequest = [NSURLRequest requestWithURL:mensaURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
NSCachedURLResponse* cachedMenuResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:menuRequest];
if (cachedMenuResponse) {
    // received data is a member of that class to which the asynchronous 
    // download writes and which is then being used in updateDataFromDownload
    // to retrieve my data structure from the download.
    self.receivedData = [NSMutableData dataWithData:[cachedMenuResponse data]];
    [self updateDataFromDownload];
    NSLog(@"using data from cache");
} else {
    NSLog(@"opening connection");
    [NSURLConnection connectionWithRequest:menuRequest delegate:self];
}
+1  A: 

AFAIK NSURLRequest/NSURLConnection on iOS doesn't support caching to disk.

Ole Begemann
the documentation says it does, or at least implies that when providing methods to set the disk capacity used by the cache.
Jan Greve
Yeah, I know. I still think it's not supported. See https://devforums.apple.com/message/182443#182443 and http://github.com/rs/SDURLCache, for example.
Ole Begemann
Thanks for that. SDURLCache offers an easy way to do exactly what i want :)
Jan Greve