0
votes

I am attempting to map my Core Data "Component" Entity to the "hotspots" keypath in this response JSON, but it only gets to the array object so it will not map. I can't tell if my problem is the mapping or the response descriptor or a combination of the two.

"result" : {
"type" : "Slidedeck",
"id" : 81,
"name" : "Brendantest",
"slides" : [
  {
    "thumb" : "http:\/\/api.idetailapp.review.thingee.com\/v4\/resources\/1294\/download\/slide?thumb=true",
    "id" : 1294,
    "notes" : "",
    "label" : "",
    "order" : 1,
    "parent_id" : 81,
    "file" : "slide-20.jpg",
    "components" : {
      "hotspots" : [
        {
          "x" : 205,
          "options" : "embedded",
          "type" : "video",
          "id" : 6082,
          "y" : 453,
          "assets" : [
            {
              "thumb" : "\/system\/asset_objects\/14\/original_thumb\/14.png",
              "id" : 14,
              "parent_id" : 6082,
              "file" : "slide_15_chart_animation_ipad_r03.mov",
              "url" : "http:\/\/api.idetailapp.review.thingee.com\/v4\/resources\/14\/download\/asset"
            }
          ],
          "parent_id" : 1294,
          "width" : 320,
          "height" : 246
        }
      ]
    },
    "url" : "http:\/\/api.idetailapp.review.thingee.com\/v4\/resources\/1294\/download\/slide"
  }
],
"version" : "0.43"

} }

Here is my RKEntityMapping:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Component"
                                               inManagedObjectStore:[RKManagedObjectStore  defaultStore]];


mapping.identificationAttributes = @[ @"componentID" ];


[mapping addAttributeMappingsFromDictionary:@{@"id": @"componentID",
                                              @"parent_id": @"parentID"
                                              }];
[mapping addAttributeMappingsFromArray:@[@"x", @"y", @"width", @"height", @"type", @"options"]];

Here is my Responsedescriptor:

componentResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:componentEntityMapping 
method:RKRequestMethodAny pathPattern:nil  
keyPath:@"result.slides.components.hotspots"         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

I put in a RestKit/ObjectMapping debug trace and get to the following output:

Asked to map source object (
    {
    assets =         (
                    {
            file = "slide_15_chart_animation_ipad_r03.mov";
            id = 14;
            "parent_id" = 6082;
            thumb = "/system/asset_objects/14/original_thumb/14.png";
            url = "http://api.idetailapp.review.thingee.com/v4/resources/14/download/asset";
        }
    );
    height = 246;
    id = 6082;
    options = embedded;
    "parent_id" = 1294;
    type = video;
    width = 320;
    x = 205;
    y = 453;
}

And further debug message of:

Failed transformation of value at keyPath 'id' to representation of type 'NSNumber': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '( 6082 )UserInfo=0x12479350 {NSLocalizedDescription=Expected an inputValue of type NSNull, but got a __NSArrayI.}" ),

1
In the JSON, will slides only ever have 1 element? (your mapping got further than I expected...) - Wain
@Wain this was just a basic JSON reply, there will usually be many slide elements - Halford

1 Answers

1
votes

Your problem is keyPath:@"result.slides.components.hotspots".

Specifically, the fact that slides is an array.

The result is that RestKit is asked to map an array of objects into your mapping. So, when it tries to get the id (which should be a number), it gets an array (hence your error). This is because calling valueForKey: on an array returns an array...

So, basically, you can't do it the way you're trying to because of the array in the middle of the JSON.

If you can get the JSON changed, do that. If you can't, you need to change your mapping to map the slides first and then the nested components (then you can discard the slides afterwards if you want).