Simone Folador
Simone Folador

Reputation: 39

RestKit - Nested Structure Fail

I am currently using RestKit in our project but I am facing some issues to manage this structure:

{"Elements": [
    {
      "Element": {
        "id": "1",
        "property": "house",
        "_addons": [
          {
            "Links": [
              {
                "Link": {
                  "id": "2",
                  "url": "http://www.google.com"
                }
              },
              {
                "Link": {
                  "id": "3",
                  "url": "http://www.google.com"
                }
              }
            ]
          }
        ]
      }
    },
    {
      "Element": {
        "id": "2",
        "property": "garage",
        "_addons": [
          {
            "Links": [
              {
                "Link": {
                  "id": "4",
                  "url": "http://www.google.com"
                }
              },
              {
                "Link": {
                  "id": "5",
                  "url": "http://www.google.com"
                }
              }
            ]
          }
        ]
      }
    }
  ]
}

Right now in the object Element, there is the property "links" which is a NSMutableArray (I tried also with `NSMutableDictionary but the problem is still there). Now, if I try to do

[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"_addons.Links.Link" toKeyPath:@"links" withMapping:[Link mapping]]];

I receive an error saying that I wasn't possibile to map and there is a collection inside a collection.

The only solution I found was to include @"_addons.Links" : @"links" in the mapping dictionary. In this case, though, I "lose" the chance to have the mapping to Link objects and I have to do it manually. Anybody has any advice?

Upvotes: 1

Views: 63

Answers (1)

Wain
Wain

Reputation: 119031

You don't say exactly what you want, but the JSON does have an array in an array so you need to deal with that somehow. Options:

  1. Change the JSON
  2. Create a custom transformer to take the array of arrays and convert into an array of objects
  3. Use 2 mappings and relationships so the nested objects in the JSON are passed over into your object graph

Generally, the first or last option is best. You are, it seems, already half way to the last option, you just need an extra mapping and relationship.

Upvotes: 1

Related Questions