Graphics Programs Reference
In-Depth Information
Using the Store
Before you implement the details of the store, you are going to implement the code in the
controller that uses
fetchRSSFeedWithCompletion:
.
ListViewController
only needs to initiate the fetch and describe what will happen in the completion block it
passes as the argument.
Right now, the
ListViewController
is responsible for creating the
NSURLRequest
and the
NSURLConnection
. It also implements the delegate methods for
NSURLCon-
nection
that accumulate the response data from the server and execute code when the re-
quest either succeeds or fails. This code needs to be moved into the store; it is request lo-
gic. You can begin by deleting the body of
fetchEntries
in
ListViewControl-
ler.m
.
- (void)fetchEntries
{
xmlData = [[NSMutableData alloc] init];
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];
connection = [[NSURLConnection alloc] initWithRequest:req
delegate:self
startImmediately:YES];
}
Now let's re-implement
fetchEntries
to leverage the store's power. At the top of
ListViewController.m
, import the header for
BNRFeedStore
.
#import "BNRFeedStore.h"
Enter the new code for
fetchEntries
in
ListViewController.m
.
- (void)fetchEntries
{
// Initiate the request...
[[BNRFeedStore sharedStore] fetchRSSFeedWithCompletion:
^(RSSChannel *obj, NSError *err) {
// When the request completes, this block will be called.
if (!err) {
// If everything went ok, grab the channel object, and
// reload the table.
channel = obj;
[[self tableView] reloadData];
} else {