Graphics Programs Reference
In-Depth Information
return [[NSUserDefaults standardUserDefaults]
objectForKey:@"topSongsCacheDate"];
}
Notice that we put this code in the store. This is because, once again, you are interacting
with an external source (the preferences file on the filesystem). Additionally, this code is
related to fetching the RSS feed.
From outside the store, topSongsCacheDate looks like a standard property - a set of
methods that access an instance variable. That's good. The store does all of this interesting
stuff while making its job look easy. Stores are kind of like Olympic athletes - you don't
see the lifetime of hard work; you just see a few seconds of effortless speed.
For a store to be able to return the cached RSSChannel , it first needs to cache it. Every
time the store fetches the top songs feed, we want it to save the channel and its items, as
well as note the time they were cached. However, the way BNRFeedStore is set up
right now, the only code that is executed when a request completes is the block supplied
by ListViewController . This means the BNRFeedStore doesn't know when the
request finishes, so it doesn't have a chance to cache the data. Thus, the store needs to add
a callback to the BNRConnection that handles the request.
We already have a way to do this: the store can create its own completion block for the
BNRConnection to execute. But alas, that spot is already reserved for the
ListViewController 's completion block. Fortunately, you can execute a block in-
side another block. So we're going to have the completion block for the store run its code
(which will take care of the caching) and then execute the completion block from the con-
troller. This one-two punch of blocks will become the completionBlock for the
BNRConnection .
In BNRFeedStore.m , update fetchTopSongs:withCompletion: .
- (void)fetchTopSongs:(int)count
withCompletion:(void (^)(RSSChannel *obj, NSError *err))block
{
// Construct the cache path
NSString *cachePath =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
cachePath = [cachePath stringByAppendingPathComponent:@"apple.archive"];
NSString *requestString = [NSString stringWithFormat:
@"http://itunes.apple.com/us/rss/topsongs/limit=%d/json", count];
NSURL *url = [NSURL URLWithString:requestString];
Search WWH ::




Custom Search