Previous | Next
WireHose Developers Guide

Cleaning snapshots

The mapping model does a fairly good job of translating the XML input into dictionaries, but it has some quirks, and WHImporter's insertResources expects those dictionaries to be in a particular format. The importer needs a cleanSnapshots method which will fix what we get from fetchDictionaryFromURL.

  1. Add this method:
    static NSMutableArray cleanSnapshots(Object whatWeFound) {
       
        NSMutableArray snapshots;
       
        // "forcelist" in the mapping model is unreliable
        // sometimes we get a single object, so pack it into an array
        if (whatWeFound instanceof NSArray) {
            snapshots = (NSMutableArray)whatWeFound;
        } else {
            snapshots = new NSMutableArray(whatWeFound);
        }
       
       
        NSMutableDictionary snapshot;
        NSKeyValueCoding tags;
       
        // iterate through snapshots
        // for each snapshot, extract tags from content key
        for (int i=0, count=snapshots.count(); i<count; i++) {
            snapshot = (NSMutableDictionary)snapshots.objectAtIndex(i);
            tags = (NSKeyValueCoding)snapshot.objectForKey("tags");
            if (tags != null) {
       
                // tags will be either a dictionary with one key "id"
                // mapping to a string indicating the tagpath, 
                // or an array of dictionaries, each with an "id" key.
       
                // Calling valueForKey on an array will construct a new
                // array with the results of calling valueForKey on each
                // object in the old array... nice.
       
                // So we end up with either a string, or an array of strings
                snapshot.setObjectForKey(tags.valueForKey("id"), "tags");
                snapshots.replaceObjectAtIndex(snapshot, i);
            }
        }
        return snapshots;
    }

    The mapping model specifies that RSS items should be mapped to a list through its forceList property. Sometimes the XML importer returns a single item instead of a list, so this method will pack the object into an array.

    The RSS 2.0 format specifies that <category> is a container element. The contentsKey property in the mapping model specifies that the XML importer should map a category to a dictionary with a single key, "id", which maps to the name of the category itself. If an item has multiple category entries, then the importer will return an array of dictionaries. The cleanSnapshots method extracts the category (or categories) from the dictionary (or dictionaries).


Previous | Next