Question

I have a Media model, and Image,Video and Audio models that inherit from media. I am using RestKit to map the data from an API(rails app).

Is there a way to do a conditional mapping using RestKit? If my response contains a media_type of video, then use the Video model mapping otherwise the image mapping etc.

Update

RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
RKObjectMapping *videoMapping = [RKObjectMapping mappingForClass:[MMXMediaVideo class]];
RKObjectMapping *imageMapping = [RKObjectMapping mappingForClass:[MMXMediaImage class]];
[dynamicMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"medium_type" expectedValue:@"video" objectMapping: videoMapping]];
[dynamicMapping addMatcher:[RKObjectMappingMatcher matcherWithKeyPath:@"medium_type" expectedValue:@"image" objectMapping:imageMapping]];

How do I add a mapping method to video or image mapping?

Was it helpful?

Solution

You configure your response descriptor with a dynamic mapping (RKDynamicMapping). This mapping checks the incoming data to decide which mapping is the appropriate one to apply and returns that. The returned mapping is then used to perform the actual destination object creation and run the mapping procedure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top