Graphics Programs Reference
In-Depth Information
// If it is less than 300 seconds (5 minutes) old, return cache
// in completion block
NSLog(@"Reading cache!");
RSSChannel *cachedChannel = [NSKeyedUnarchiver
unarchiveObjectWithFile:cachePath];
if (cachedChannel) {
// Execute the controller's completion block to reload its table
block(cachedChannel, nil);
// Don't need to make the request, just get out of this method
return;
}
}
}
NSString *requestString = [NSString stringWithFormat:
@"http://itunes.apple.com/us/rss/topsongs/limit=%d/json", count];
Build and run the application. Select the Apple item on the segmented control and notice
the console says it is reading from the cache. The list will appear immediately, and no re-
quest will be made - you won't see the activity indicator at all when switching to the
Apple feed. (You can check again in five minutes and see that the feed is indeed fetched
again. Or delete the application and run it again.)
There is a slight issue here, although you can't see it. When ListViewController
makes this request, it expects some time to pass before the request is completed - this is
why it supplies a callback block to be executed when the request is finished. When the
store decides to return cached data, the callback is executed immediately. This can cause
an issue if the ListViewController has more code in fetchEntries after it
makes the request because it expects that code to be executed before the completion
block.
Let's put in a two log statements in ListViewController.m to confirm this behavi-
or.
- (void)fetchEntries
{
UIView *currentTitleView = [[self navigationItem] titleView];
UIActivityIndicatorView *aiView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[[self navigationItem] setTitleView:aiView];
[aiView startAnimating];
void (^completionBlock)(id obj, NSError *err) =
^(RSSChannel *obj, NSError *err) {
NSLog(@"Completion block called!");
[[self navigationItem] setTitleView:currentTitleView];
if (!err) {
channel = obj;
[[self tableView] reloadData];
Search WWH ::




Custom Search