Graphics Programs Reference
In-Depth Information
Caching the RSS Feed
Each time the user fetches the Apple top songs RSS feed, the BNRFeedStore makes a
web service request to Apple's server. However, it takes time and battery power to make
these requests. The data in this feed doesn't change very often, either. Therefore, it doesn't
make much sense to make the resource-consuming request over and over again.
Instead, the store should save the results of the last request made. If the user tries to reload
the feed within five minutes, the store won't bother making the request and will just return
the saved results. This process is known as caching .
Generally speaking, a cache is data that has been saved in another place where it is easier to
retrieve. For example, it is easier to retrieve data from the local filesystem than from a serv-
er somewhere out in Internet land. In Nerdfeed , the cache will be the RSSChannel and
the RSSItem s that represent the top ten songs. We will store this cache on the filesystem,
so RSSChannel and RSSItem must be able to be saved and loaded.
For saving and loading these objects, it makes sense to use archiving instead of Core Data
for three reasons. First, we won't have that many records to save and load (just ten). Se-
cond, we don't have a lot of relationships between objects (just a channel containing an ar-
ray of items). Third, archiving is easier to implement.
Before you can write the code to save and load the cache, RSSChannel and RSSItem
have to conform to NSCoding . In RSSChannel.h , declare that this class conforms to
NSCoding .
@interface RSSChannel : NSObject
<NSXMLParserDelegate, JSONSerializable , NSCoding >
In RSSChannel.m , implement the two NSCoding methods.
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:items forKey:@"items"];
[aCoder encodeObject:title forKey:@"title"];
[aCoder encodeObject:infoString forKey:@"infoString"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
items = [aDecoder decodeObjectForKey:@"items"];
Search WWH ::




Custom Search