Restkit: including field from request into object mapping

108 views Asked by At

I am making rest call as this

GET /entities?parent=123
[
    {id:1, name:"name 1"},
    {id:2, name:"name 2"}
]

My entity defination have following fields

id
name
parent

The problem is that parent field is not coming in the response, It is in the request. How can I save parent field from request to coredata?

I have tried to search these things with no luck.

  • I have tried looking for some transformers which could transform response before being processed by restkit but could not find anything.
  • I have seen Metadata Mapping in RKObjectRequestOperation but could not figure out if/how this can be used.

thanks

EDIT

Answer provided below works only if that is used with object manager. So the following works

RKDynamicMapping *learningObjectMapping = [MTAbstractLearningObject map];//this contains metadata mapping for @metadata.routing.parameters.entityId
RKResponseDescriptor* learningObjRd = [RKResponseDescriptor responseDescriptorWithMapping:learningObjectMapping 
    method:RKRequestMethodGET pathPattern:@"entity/:entityId" keyPath:@"objects" 
    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:learningObjRd];
[self.objectManager.router.routeSet addRoute:[RKRoute routeWithName:@"learningObjects" pathPattern:@"entity/:id" method:RKRequestMethodGET]];

If object manager is constructed as shown above, and requests are made as shown below.

[self.objectManager getObjectsAtPathForRouteNamed:@"learningObjects" object:entity parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"API: Success got learning objects");
}failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"API: Failed to get learning objects");
}];

Then it will work.

1

There are 1 answers

4
Wain On

You can use metadata, it includes a dictionary of query parameters that you can access. Your mapping will contain

@"@metadata.query.parameters.parent": @"parent"