Graphics Programs Reference
In-Depth Information
}
Now an RSSItem can be ordered amongst its peers by publication date. In RSSChan-
nel.m , modify addItemsFromChannel: to reorder its items.
- (void)addItemsFromChannel:(RSSChannel *)otherChannel
{
for (RSSItem *i in [otherChannel items]) {
if (![[self items] containsObject:i])
[[self items] addObject:i];
}
// Sort the array of items by publication date
[[self items] sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj2 publicationDate] compare:[obj1 publicationDate]];
}];
}
The method sortUsingComparator: is a nifty tool for sorting an NSMutableAr-
ray . This method takes a block that takes two object pointers as an argument and returns
an NSComparisonResult . (An NSComparisonResult is a data type whose value
can tell us the sorted order of two objects.) When this method executes, it compares each
item in the array by passing two of them at a time as arguments to the supplied block. The
block returns which of the two objects is ordered before the other as an NSComparis-
onResult .
The NSDate method compare: compares two NSDate instances and returns the one
that is ordered before the other as an NSComparisonResult . The return value of
compare: is the return value of the comparator block.
Now that a channel can update its items array when given a new channel, let's have the
store take advantage of this. In BNRFeedStore.h , change the return type of
fetchRSSFeedWithCompletion: .
- (void)fetchRSSFeedWithCompletion:(void (^)(RSSChannel *obj, NSError
*err))block
- (RSSChannel *)fetchRSSFeedWithCompletion:
(void (^)(RSSChannel *obj, NSError *err))block;
In BNRFeedStore.m , update the completion block for fetchRSSFeedWithCom-
pletion: to merge the incoming channel with the existing channel and cache it.
- (void)fetchRSSFeedWithCompletion:(void (^)(RSSChannel *obj, NSError
*err))block;
- (RSSChannel *)fetchRSSFeedWithCompletion:
(void (^)(RSSChannel *obj, NSError *err))block
 
Search WWH ::




Custom Search