Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created October 9, 2012 15:08
Show Gist options
  • Save lukeredpath/3859420 to your computer and use it in GitHub Desktop.
Save lukeredpath/3859420 to your computer and use it in GitHub Desktop.
{
"user": {
"id": 999,
"name": "Joe Bloggs"
},
"stream": [
{
"link": {
"url": "http://www.example.com",
"id": 123
}
},
{
"status": {
"text": "Hellow",
"id": 123
}
},
{
"photo": {
"url": "http://www.example.com/photo.jpg",
"id": 123
}
}
]
}
@implementation SomeClassRepresentingJustThisResponse
// has properties for user, streamItems;
+ (RKObjectMapping *)objectMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:self];
// this one works
RKRelationshipMapping *eventMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:[User objectMapping]];
[mapping addPropertyMapping:eventMapping];
/* one mapping for each type of supported stream item */
NSDictionary *streamMappings = @{
@"image": [Image objectMapping],
@"link": [Link objectMapping],
@"status": [Status objectMapping]
};
RKDynamicMapping *dynamicPrerollItemMapping = [[RKDynamicMapping alloc] init];
[dynamicPrerollItemMapping setObjectMappingForDataBlock:^RKObjectMapping *(NSDictionary *data) {
NSString *type = [[data allKeys] objectAtIndex:0];
return [prerollMappings objectForKey:type];
}];
// this one gives me a collection of the right objects, but no mapped attributes
RKRelationshipMapping *prerollItemsMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"stream" toKeyPath:@"streamItems" withMapping:dynamicPrerollItemMapping];
[mapping addPropertyMapping:prerollItemsMapping];
return mapping;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment