Graphics Programs Reference
In-Depth Information
Building BNRFeedStore
The code that was removed from ListViewController must be replaced in
BNRFeedStore . The BNRFeedStore must handle preparing the NSURLRequest ,
kicking off the NSURLConnection , handling the response from the server, and starting
the parser. The ListViewController simply asks for these actions to be initiated and
supplies a callback for when they are completed.
Initiating the connection
A store's job is to take a really simple request from a controller, like “fetch the RSS feed,”
and prepare a detailed request tailored for the external source it interacts with. In this case,
the detailed request is the web service call to http://forums.bignerdranch.com .
In BNRFeedStore.m , begin implementing fetchRSSFeedWithCompletion: by
preparing this request.
- (void)fetchRSSFeedWithCompletion:(void (^)(RSSChannel *obj, NSError *err))block
{
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];
}
It is important that the NSURLRequest is created inside the store and not in
ListViewController . It is not ListViewController 's job to understand how to
interact with the web server; thus, it shouldn't know about the URL or the arguments
passed in that URL. In fact, the ListViewController shouldn't even know that the
Internet exists or what an NSURL is. It just wants an RSSChannel , and by golly, it'll get
one.
The store's next step is to create an NSURLConnection to process this
NSURLRequest . Your first intuition would be to create the NSURLConnection here in
fetchRSSFeedWithCompletion: and set the delegate of the connection as the
BNRFeedStore . However, what happens when we add more requests to the store?
For example, the store could pull the RSS feed from another server. The store, then, would
have to be the delegate for a lot of NSURLConnection s. The NSURLConnection del-
Search WWH ::




Custom Search