I am currently downloading and caching several different data sets from my server. However I am swapping all of my code from ASI over to NSURLConneciton stuff. and I am at the point where I am ready to pass my caches over to my parser delegates however they accept a NSData type as a parameter
- (void)startTheParsingProcess:(NSData *)parserData
where my cache is of type
NSCachedURLResponse
as you can see here
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
if (dataSetToParse == @"a"){
NSCachedURLResponse *aCachedResponse = cachedResponse;
aCachedResponse = nil;
NSDictionary *newUserInfo;
newUserInfo = [NSDictionary dictionaryWithObject:[NSDate date] forKey:@"Cached Date"];
aCachedResponse = [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:[cachedResponse data] userInfo:newUserInfo storagePolicy:[cachedResponse storagePolicy]];
NSLog(@"%@", aCachedResponse);
return aCachedResponse;
}
if (dataSetToParse == @"b"){
NSCachedURLResponse *bCachedResponse = cachedResponse;
bCachedResponse = nil;
NSDictionary *newUserInfo;
newUserInfo = [NSDictionary dictionaryWithObject:[NSDate date] forKey:@"Cached Date"];
bCachedResponse = [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:[cachedResponse data] userInfo:newUserInfo storagePolicy:[cachedResponse storagePolicy]];
NSLog(@"%@", bCachedResponse);
return bCachedResponse;
}
return nil;
}
any example code would be helpfull thanks or just pointing me in the right direction 🙂
I am looking at the NSCachedURLResponse definition in the apple docs, and it says that NSCachedURLResponse has the data that I want I’m just not sure how I can get to it… to use with my parser delegates.
You mean something like that?
NSCachedURLResponsehas also a direct methoddata, but I cannot test if it’s the same result as the code above.Cache suggestion
I see that you have your own methods that deal with caching. I would suggest subclassing the NSURLCache, if you need to modify caching and, say, add an offline mode. If you subclass it and implement some methods, like (but not limited to):
life may become easier for you. You would just set global app cache in application delegate through
[NSURLCache setSharedURLCache:[CustomURLCache new]];call.Then all your standard NSURL-whatever classes would behave as if they work with system cache, and you normally won’t have to deal with direct calls to get or store the cache, the system would call them automatically.