tags:

views:

37

answers:

1

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) { }

+1  A: 

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:

FeedItem* item;
NSMutableArray* allItems;
NSMutableDictionary* itemIndex;

// if this is not the first run, load existing stuff into allItems and itemIndex
// set up the parser

while( item = [parser nextItem] ) {
  FeedItem* existingItem = [itemIndex objectForKey:[item uniqueKey]];
  if( existingItem == nil ) {
    // haven't seen this item before
    [allItems addObject:item];
    [itemIndex setObject:item forKey:[item uniqueKey]];
  }
  else {
    // done parsing
    break;
  }
}

// clean up parser, save items to disk, etc.

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.)

Sixten Otto
This still doesn't address my question about iterating through the table. The types of items I am parsing are never updated. They are simply links. I can use the URL for uniqueness but again... I still would like to know the best approach for testing equality. Not what fields I should use... but what approach to take.
radesix
I'm suggesting that instead of attempting to use your array to both store the sequence of items *and* determine which ones you've already seen, that you use two data structures: the array you have now to store your items in order, and *also* a dictionary. I'll edit some pseudo-code into my answer to illustrate.
Sixten Otto
I think I see what you are saying... i'll give this a shot! Thanks!
radesix
This solution was exactly what i needed. Thanks for your help!
radesix