Skip to content

Data Processor plugin

eXaLy edited this page Jan 28, 2013 · 8 revisions

The task of the Data Processor is to read the JSON data with a specific encoding structure from a DataSource. When the app wants to download from the DataSources, the app will try to match the right DataProcessor with the right DataSource. With the read data, the DataSourceManager can create the Position objects for the DataSource object. The read data will be stored and returned in a NSMutableArray with multiple NSDictionary inside. There are already examples in the dataprocessor folder (Wikipedia, Twitter...), but you can read the explanation here below.

Create your own DataProcessor

  • Create your plugin class
  • Import Mixare library #import <Mixare/Mixare.h>
  • In your .h file, use the DataProcessor protocol.
  • If your processor uses JSON, you should use JsonData as superclass.

Example

@interface YourProcessorClass : JsonData <DataProcessor>

How to implement code in .m file

- (BOOL)matchesDataType:(NSString*)title;

You should match the parameter with the title of the specific DataSource you want to match. If it’s true, return YES.

- (NSString*)createDataString:(NSString*)jsonUrl location:(CLLocation*)loc radius:(float)rad;

You can manipulate your datasource-url with actual longitude, latitude, altitude and radius data if your datasource-url has parameters. You don’t have to use this method if your plugin uses JsonData as superclass.

- (NSMutableArray*)convert:(NSString*)dataString;

The method what it’s all about. Converts the data from the datasource-url to useable data in array. The array should have dictionaries with specific key-names. The key-names (which will be detected to use) are: title, lat, lon, alt, sum, url, user, source, imagemarker, reference, logo. Title, latitiude, longitude, altitude are required. What you should do is, link the JSON data from the different keyname to the Mixare’s universal key-names and return this array with the dictionaries.

Example

- (NSMutableArray*)convert:(NSString*)dataString {
    if (dataString != nil) {
        NSDictionary *data = [dataString JSONValue];
        NSMutableArray *ret = [[NSMutableArray alloc] init];
        NSArray *geonames = data[@"results"];
        for (NSDictionary *geoname in geonames) {
            [ret addObject:@{
             @"title": geoname[@"title"],
             @"sum": geoname[@"shorttext"],
             @"url": geoname[@"webpage"],
             @"lon": geoname[@"lng"],
             @"lat": geoname[@"lat"],
             @"imagemarker": geoname[@"image"]}];
        }
        return ret;
    }
    return nil;
}

Take a look in the extensions folder of Mixare to find more examples.

Clone this wiki locally