My objective is to abort the NSXMLParser when I parse an item that already exists in cache. The basic flow of the program works like this:
1) Program starts and downloads an XML feed. Each item in the feed is represented by a custom object (FeedItem). Each FeedItem gets added to an array.
2) When the parsing is complete the contents of the array (all FeedItem objects) are archived to the disk.
The next time the program is executed or the feed is refreshed by the user I begin parsing again; however, since a cache (array) now exists as each item is parsed I want to see if the object exists in the cache. If it does then I know I have downloaded all the new items and no longer need to continue parsing.
What I am learning, I think, is that I can’t use indexOfObject or indexOfObjectIDenticalTo: because these really seem to be checking to see that the objects are using the same memory address (thus identical).
What I want to do is see if the contents of the object are equal (or at least some of the contents). I’ve done some research and found that I can override the IsEqual method; however, I really don’t want to iterate/enumerate through the entire cache contents table for every newly parsed XML FeedItem. Is iterating through the collection and testing each one for equality the only way to do this or is there a better technique I am not aware of?
Currently I am using the following code though I know it needs to change:
NSUInteger index = [self.feedListCache.feedList indexOfObject:self.currentFeedItem];
if (index == NSNotFound) {
}
As an alternative, perhaps you could also maintain a dictionary of items. IIRC, RSS and Atom both specify that each item has a unique key (or maybe just a link, in the case of RSS?). If you use those unique identifiers as the dictionary keys, that would give you a quick way to determine whether you’ve already seen an item that matches the one you just parsed.
So, in psuedocode, it might work something like this:
A warning, though: it is possible for someone to update an item in a feed (say, change the title after it’s been published). So you might not necessarily want to stop parsing as soon as you see an item that you’ve seen before. One refinement to my suggestion above might be to compare the publication dates of the cached item and the newly-parsed item. (But even that isn’t foolproof.)