Graphics Programs Reference
In-Depth Information
Creating BNRFeedStore
Right now, the request logic in
Nerdfeed
is in a controller. Open
Nerdfeed.xcodeproj
and locate
ListViewController.m
.
ListViewController.m
is a big file already, and adding new features to this class
will only get more difficult as it becomes larger. However, if you look at the code in this
class, you can break it into two categories. First, there is the code that deals with populating
views with model objects and handles user interaction. These are the data source and deleg-
ate methods for
UITableView
, the action methods, and the methods that control the flow
of the application, like
fetchEntries
.
The other code in this file is request logic - preparing an
NSURLConnection
, receiving
its response, and handling the parsing of the data that returns from the server. In MVCS, a
controller should not be responsible for this code, so we're going to create a store object to
take over these responsibilities.
Create a new
NSObject
subclass and name it
BNRFeedStore
. This object will be the
point of contact between your controllers and the server that hosts the BNR forum and RSS
feed.
A store object is typically a singleton. Because
BNRFeedStore
is a singleton, all control-
lers access the same instance of
BNRFeedStore
, which allows us to easily cache and
store information about the server in one place. In
BNRFeedStore.h
, declare a new
method that returns the single instance of
BNRFeedStore
.
@interface BNRFeedStore : NSObject
+ (BNRFeedStore *)sharedStore;
@end
In
BNRFeedStore.m
, implement this method.
+ (BNRFeedStore *)sharedStore
{
static BNRFeedStore *feedStore = nil;
if (!feedStore)
feedStore = [[BNRFeedStore alloc] init];
return feedStore;
}
When
Nerdfeed
launches, it fetches the RSS feed. The container object for the RSS feed is
an instance of
RSSChannel
, and each individual post is represented by an
RSSItem