Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLRequest Cache Policy

I am using NSURLRequest with CachePolicy to download a plist in NSData. When I change the content of my plist my app is ignoring this and still presents the content which is cached. How long does the cache persist? If so is there an option to say how long the cache data persists? Is there a way to check in NSURLRequest if the data on the server is newer than the cache load the data from the server or if it is equal to cache use the cache?

like image 321
halloway4b Avatar asked Nov 05 '22 01:11

halloway4b


1 Answers

Have a look at Controlling Response Caching in the URLLoadingSystem docs.

You can add your own date in the delegate methods

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

Much more easy with the caching system is ASIHTTPRequest. I recommend to use this URL Loading System.

From the apple docs:

The example in Listing 6 prevents the caching of https responses. It also adds the current date to the user info dictionary for responses that are cached.

-(NSCachedURLResponse *)connection:(NSURLConnection *)connection
                 willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    NSCachedURLResponse *newCachedResponse = cachedResponse;

    if ([[[[cachedResponse response] URL] scheme] isEqual:@"https"]) {
        newCachedResponse = nil;
    } else {
        NSDictionary *newUserInfo;
        newUserInfo = [NSDictionary dictionaryWithObject:[NSCalendarDate date]
                                                 forKey:@"Cached Date"];
        newCachedResponse = [[[NSCachedURLResponse alloc]
                                initWithResponse:[cachedResponse response]
                                    data:[cachedResponse data]
                                    userInfo:newUserInfo
                                    storagePolicy:[cachedResponse storagePolicy]]
                            autorelease];
    }
    return newCachedResponse;
}
like image 170
Jonas Schnelli Avatar answered Nov 09 '22 14:11

Jonas Schnelli