3

My backend returns this kind of JSON files:

{ "response": { "message": "Test" }, "responseCode": 205 }

Depending on responseCode value, what is inside response is different. I know I have to use something like RKDynamicMapping, but not sure how. The problem I see is that in the sample code and manual, the attribute to differentiate among mappings is inside, but in this case is outside. I tried this but doesn't work:

    [dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
        NSNumber* responseCode=(NSNumber*)representation[@"responseCode"];
        if (responseCode.integerValue==204) {
            return searchByLocatorResponseContent204Mapping;
        } else if (responseCode.integerValue==205) {
            return searchByLocatorResponseContent205Mapping;
        }
        return nil;
    }];
    RKResponseDescriptor *searchByLocatorResponseContentResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping pathPattern:kCheckinSearchByLocatorPath keyPath:nil  statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptor:searchByLocatorResponseContentResponseDescriptor];

I guess because the mapping I want to change doesn't containt the attribute to distinguish them (like type in girls/boys sample code from rest kit website)

Does anyone have any recommendation? Thanks in advance.

Ricardo
  • 2,831
  • 4
  • 29
  • 42
  • Can you not use the keypath to specify the mapping for the items inside 'response'? – Wain Apr 26 '13 at 16:40

1 Answers1

0

If you set it up like this, restkit expects the dynamicMapping will provide mapping for the whole response json, not just the contents of 'response'.

Basically searchByLocatorResponseContent204Mapping needs to have mapping for the response field and the responseCode field.

As far as I know, restkit cannot look outward, since the representation parameter only gives you info for inside the current context. However, it is on the road map and there is a ticket about this already: https://github.com/RestKit/RestKit/issues/1327

Kevin R
  • 8,230
  • 4
  • 41
  • 46