0

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.}" ),

2
  • In the JSON, will slides only ever have 1 element? (your mapping got further than I expected...) Commented Oct 22, 2013 at 22:37
  • @Wain this was just a basic JSON reply, there will usually be many slide elements Commented Oct 22, 2013 at 23:23

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

how should I go about mapping to slides first and then the nested hotspots component?
You will need a new mapping and entity, change the response descriptor to use the new mapping, add relationship between the 2 entities and the 2 mappings.
can you confirm that I'm working the right direction, first I would create an RKEntityMapping and Entity for slides change the response descriptor to reflect slides. Create Entity and mapping for hotspots that will include the relationship between the two. Then after I do a Request Operation for slides the hotspots entity will get its data due to the defined relationship, no need for a separate Request Operation to get data for hotspots. Does that seem correct? Or am I missing anything? Thanks
The mapping you have is ok for components (you don't need a hotspots named one), you just need to add the relationship. Yes on the other entity and mapping. For the response descriptor, remember to change the key path. Correct about the loading without a second request (because the data is nested).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.