Graphics Programs Reference
In-Depth Information
NSURLRequest *req = [NSURLRequest requestWithURL:url];
RSSChannel *channel = [[RSSChannel alloc] init];
BNRConnection *connection = [[BNRConnection alloc] initWithRequest:req];
[connection setCompletionBlock:block];
[connection setCompletionBlock:^(RSSChannel *obj, NSError *err) {
// This is the store's completion code:
// If everything went smoothly, save the channel to disk and set cache date
if (!err) {
[self setTopSongsCacheDate:[NSDate date]];
[NSKeyedArchiver archiveRootObject:obj toFile:cachePath];
}
// This is the controller's completion code:
block(obj, err);
}];
[connection setJsonRootObject:channel];
[connection start];
}
You can build and run the application now. While Nerdfeed can't read the cache yet, it is
able to archive the channel and note the date after you select the Apple feed. You can veri-
fy this by running the application on the simulator and locating the application's directory
in the simulator's Application Support directory. (If you forgot how, flip back to
Chapter 14 and the section called “NSKeyedArchiver and NSKeyedUnarchiver” .) You
can check the preferences file in Library/Preferences/ for the topSong-
sCacheDate key and look for the archive in Library/Caches/ .
The store now needs to return the cached data if it is still fresh. It should do this without
letting the controller know it is getting cached data - after all, the controller doesn't really
care; it just wants the top ten songs. In BNRFeedStore.m , update
fetchTopSongs:withCompletion: to do this.
- (void)fetchTopSongs:(int)count
withCompletion:(void (^)(RSSChannel *obj, NSError *err))block
{
NSString *cachePath =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
cachePath = [cachePath stringByAppendingPathComponent:@"apple.archive"];
// Make sure we have cached at least once before by checking to see
// if this date exists!
NSDate *tscDate = [self topSongsCacheDate];
if (tscDate) {
// How old is the cache?
NSTimeInterval cacheAge = [tscDate timeIntervalSinceNow];
if (cacheAge > -300.0) {
 
Search WWH ::




Custom Search