Graphics Programs Reference
In-Depth Information
// its contents
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:container];
[parser setDelegate:[self xmlRootObject]];
[parser parse];
}
// Then, pass the root object to the completion block - remember,
// this is the block that the controller supplied.
if ([self completionBlock])
[self completionBlock]([self xmlRootObject], nil);
// Now, destroy this connection
[sharedConnectionList removeObject:self];
}
Remember that
connectionDidFinishLoading:
is called when the connection
finishes successfully; so, the root object is delivered to the
completionBlock
, and no
NSError
is passed. If there is a problem with the connection, the opposite needs to oc-
cur: the completion block is called without the root object, and an error object is passed
instead. In
BNRConnection.m
, implement the method that handles the connection fail-
ing.
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// Pass the error from the connection to the completionBlock
if ([self completionBlock])
[self completionBlock](nil, error);
// Destroy this connection
[sharedConnectionList removeObject:self];
}
Build and run the application. After a moment, you will see the
ListViewControl-
ler
's table view populate. If there is a problem, you will see an alert view.
You be wondering why
BNRConnection
's
completionBlock
takes an argument of
type
id
instead of say,
RSSChannel
. The answer is simple:
BNRConnection
doesn't
know anything about the type of its root object except that it receives
NSXMLParser-
Delegate
messages. Nor should it. This makes the
BNRConnection
usable in any
situation that requires fetching XML data with
NSURLConnection
.
Now, at the end of the day, the changes we've made to
Nerdfeed
so far give us the exact
same behavior as we had before. You might be wondering, “Why fix what ain't broke?”
Well, in the rest of this chapter and the next two, you will be adding features to
Nerdfeed
.
Using a store object is going to make implementing those features a lot easier. (Also, the