Graphics Programs Reference
In-Depth Information
store will create an empty
RSSChannel
and give it to the
BNRConnection
. When the
connection finishes, the
BNRConnection
will parse the data from the
NSURLConnec-
tion
into the
xmlRootObject
before handing it to the controller.
Before we write the code for
BNRConnection
, we will finish
fetchRSSFeedWithCompletion:
in
BNRFeedStore.m
. First, import the appro-
priate files into
BNRFeedStore.m
.
#import "RSSChannel.h"
#import "BNRConnection.h"
In
BNRFeedStore.m
, add code to
fetchRSSFeedWithCompletion:
that creates
an instance of
BNRConnection
and sets it up with the request, completion block and
empty root object.
- (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];
// Create an empty channel
RSSChannel *channel = [[RSSChannel alloc] init];
// Create a connection "actor" object that will transfer data from the server
BNRConnection *connection = [[BNRConnection alloc] initWithRequest:req];
// When the connection completes, this block from the controller will be called
[connection setCompletionBlock:block];
// Let the empty channel parse the returning data from the web service
[connection setXmlRootObject:channel];
// Begin the connection
[connection start];
}
We are now at the heart of the code that will transform
Nerdfeed
from a pedagogical ex-
ample to a functional powerhouse: the code for
BNRConnection
. We've changed the
code in
Nerdfeed
by working from the outside and moving in, allowing us to see the sim-
plicity earned by using MVCS and then the work required for that simplicity. We are al-
most there.
An instance of
BNRConnection
, when started, will create an instance of
NSURLCon-
nection
, initialize it with an
NSURLRequest
, and set itself as the delegate of that con-
nection. In
BNRConnection.m
, implement the following two methods and synthesize
the three properties.
@implementation BNRConnection