bevinlorenzo
bevinlorenzo

Reputation: 496

How to add additional attributes to RestKit Mapping that are not in JSON

I have implemented a Restkit mapping to core data that contains an attribute of 'timestamp'. My local data model supports 'day', 'month', and 'year' integer attributes that I need to populate using the timestamp from JSON. I am not sure where and how to do this.

My Mapping:

RKEntityMapping *mediaMapping = [[VSObjectStore shared] mappingForEntityForName:@"Media"];
    [mediaMapping setIdentificationAttributes:@[@"id"]];

    // Remove any relationships
    NSMutableArray *mediaMethods = [NSMutableArray arrayWithArray:[_VSMedia propertyNames]];
    [mediaMapping addAttributeMappingsFromArray:mediaMethods];

Thanks in advance for your help!

Upvotes: 0

Views: 210

Answers (2)

Wain
Wain

Reputation: 119021

You should change your data model (or at least your approach). Store an NSDate instance in your model - it is the most accurate and well supported choice. Then the mapping should happen automatically. If you still want your 3 properties for day, month and year, fine, but make them derived (transient) and get them when required (or of fetch / save) from the NSDate timestamp.

Upvotes: 1

slecorne
slecorne

Reputation: 1718

You should implement the RKObjectLoaderDelegate following method:

- (void)objectLoader:(RKObjectLoader*)loader willMapData:(inout id *)mappableData;

This method is called after parsing the data but before mapping. Thus you could modify the data to add day, month and year.

The code will look something like this:

- (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout id *)mappableData
{
    NSMutableDictionary *mediaData = *mappableData;
    // parse timestamp here
    [mediaData setObject:day forKey:@"day"];

}

and add the key to your mapping list:

NSMutableArray *mediaMethods = [NSMutableArray arrayWithArray:[_VSMedia propertyNames]];

[mediaMethods addObject:@"day"]



[mediaMapping addAttributeMappingsFromArray:mediaMethods];

Upvotes: 1

Related Questions