iosarraysrestkitresponsepartial

RESTKIT response only first KVC in mapping result dictionary or array


I'm receiving a dictionary from my restkit request as respond but only one of values are included. I am missing bunch of KV from my JSON.

JSON response from API:

{"categories" : [
{
  "status" : 1,
  "rest_id" : 1,
  "id" : 1,
  "title" : "01. BasicC",
  "description" : "basic description"
},
{
  "status" : 1,
  "rest_id" : 1,
  "id" : 26,
  "title" : "01. Deli",
  "description" : "deli description"
}
]}

IOS Function to request:

- (void)loadProduct{
_categoriesDic=[[NSDictionary alloc]init];
RKObjectMapping* productMapping = [RKObjectMapping mappingForClass:[ProductCategory     class]];
[productMapping addAttributeMappingsFromDictionary:@{
                                                 @"id": @"id",
                                                 @"rest_id": @"rest_id",
                                                 @"title": @"title",
                                                 @"description": @"description",
                                                 @"status":@"status"
                                                 }];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productMapping method:RKRequestMethodPOST pathPattern:nil keyPath:@"categories" statusCodes:[NSIndexSet indexSetWithIndex:200]];

RKObjectManager *objectManager = [[RKObjectManager alloc] init];
[objectManager addResponseDescriptor:responseDescriptor];

NSString *jsonRequest = [NSString stringWithFormat:@"id=1"];
NSURL *url=[NSURL URLWithString:@"http://example.com/REST/v2/productCategories"];
NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Categories: %@", mappingResult.dictionary);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
}

response:

Load collection of Categories: {
categories =     (
    "basic description",
    "deli description"
);
}

Solution

  • While your code does some slightly odd things (like create an empty immutable dictionary, and POST to an endpoint which should be using a GET) it does appear to work. It looks like you just aren't fully understanding the response, and that's because you are inadvertently overriding a built in method.

    The log you show contains:

    {
        categories =     (
            "basic description",
            "deli description"
        );
    }
    

    is the description of a dictionary which contains 1 key (categories), which is an array of 2 objects. Now, those 2 objects have their description method called as well in order to generate the log contents. Unfortunately, you have a property called description so that is accessed instead of the superclass implementation., and hence you just get the descriptions.

    Now, that doesn't mean the mapping hasn't worked, just that the log is misleading.

    You should change the description on your destination object to another name, like overview and then your log will be meaningful (and you won't see similar issues in future).