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?
Asked
Active
Viewed 4,038 times
1 Answers
3
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;
}

Jonas Schnelli
- 9,965
- 3
- 48
- 60
-
Thanks.I set it now to nil. But how to make the cache stay for a certain time or check if there is newer data from the server available? – halloway4b Apr 20 '12 at 18:05
-
iOS 6.1 SDK Documentation states the following: connection:willCacheResponse: Sent before the connection stores a cached response in the cache, to give the delegate an opportunity to alter it. (required) (Available in iOS 2.0 through iOS 4.3.) – cynistersix Jun 26 '13 at 19:18