Graphics Programs Reference
In-Depth Information
{
NSURL *url = [NSURL URLWithString:@"http://forums.bignerdranch.com/"
@"smartfeed.php?limit=1_DAY&sort_by=standard"
@"&feed_type=RSS2.0&feed_style=COMPACT"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
RSSChannel *channel = [[RSSChannel alloc] init];
BNRConnection *connection = [[BNRConnection alloc] initWithRequest:req];
[connection setCompletionBlock:block];
NSString *cachePath =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
cachePath = [cachePath stringByAppendingPathComponent:@"nerd.archive"];
// Load the cached channel
RSSChannel *cachedChannel =
[NSKeyedUnarchiver unarchiveObjectWithFile:cachePath];
// If one hasn't already been cached, create a blank one to fill up
if (!cachedChannel)
cachedChannel = [[RSSChannel alloc] init];
[connection setCompletionBlock:^(RSSChannel *obj, NSError *err) {
// This is the store's callback code
if (!err) {
[cachedChannel addItemsFromChannel:obj];
[NSKeyedArchiver archiveRootObject:cachedChannel
toFile:cachePath];
}
// This is the controller's callback code
block(cachedChannel, err);
}];
[connection setXmlRootObject:channel];
[connection start];
return cachedChannel;
}
Let's follow the logic of this method. When the request is made, the cached RSSChan-
nel is loaded from the filesystem. A second, empty instance of RSSChannel is set as
the xmlRootObject of the connection. When the service completes, the xmlRootOb-
ject will contain the new items from the server. This channel is then merged into the ex-
isting cachedChannel ( Figure 29.3 ). The merged channel is cached and passed to the
controller via its completion block. At the end of this method, the cached channel (before
being merged) is returned to the controller.
Figure 29.3 Merging RSSChannels
 
 
Search WWH ::




Custom Search