Graphics Programs Reference
In-Depth Information
So we could use delegation to return the
RSSChannel
from the store, but here we run
into another problem.
BNRFeedStore
is a singleton, so delegation won't work. Multiple
controllers may want to make requests to the
BNRFeedStore
at the same time, but only
one can be the delegate.
This is where blocks come in handy.
ListViewController
can make a request and
supply a block to be called when the request is complete. The store will execute this block
with the newly fetched
RSSChannel
as an argument. The block's code will set the
channel
of the
ListViewController
and reload the
UITableView
. In
BNRFeedStore.h
, declare a new method and forward declare
RSSChannel
.
@class RSSChannel;
@interface BNRFeedStore : NSObject
+ (BNRFeedStore *)sharedStore;
- (void)fetchRSSFeedWithCompletion:(void (^)(RSSChannel *obj, NSError
*err))block;
@end
When a controller wants the RSS feed, it will send the message
fetchRSSFeedWithCompletion:
to
BNRFeedStore
. This is a very simple re-
quest for the controller, but a fairly complicated one for the
BNRFeedStore
. The con-
troller doesn't care, though,
how
the RSS feed is fetched, only that it is fetched. The com-
plications are the responsibility of the store object.
Take a look at the form of the block that will be called when the request finishes. It takes
two arguments: a pointer to an
RSSChannel
and a pointer to an
NSError
object. If the
request was a success, the
RSSChannel
will be passed as an argument, and the
NSEr-
ror
will be
nil
. If there was a problem, an instance of
NSError
will be passed as an
argument, and the
RSSChannel
will be
nil
.
Let's hold off on the implementation of
fetchRSSFeedWithCompletion:
for now
and just implement it with an empty body in
BNRFeedStore.m
.
- (void)fetchRSSFeedWithCompletion:(void (^)(RSSChannel *obj, NSError *err))block
{
// We'll fill this out later
}