Search code examples
iosobjective-crestkit

Restkit: Handling dynamic nesting attributes only returns one object


I receive JSON code that looks like this:

{
  "rent": {
    "items": [

    ],
    "total": 0
  },
  "upcoming": {
    "items": [

    ],
    "total": 0
  },
  "watchnow": {
    "items": [

    ],
    "total": 0
  }
}

I want the keys, i.e. "rent", "upcoming" and "watchnow" as a property on the mapped object, so I add a NSString property called searchSection to the class I'm using and then create this mapping:

RKObjectMapping *searchResultsMapping = [RKObjectMapping mappingForClass:[TDXSearchResults class]];
[searchResultsMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"searchSection"];

Shouldn't my mappingResult.array then contain three TDXSearchResults objects, each with either "rent", "upcoming" or "watchnow" in its searchSection property? I only get one TDXSearchResults in the array and this confuses me greatly.


Solution

  • You should only expect 1 result because your source data is only 1 object (and you must only have 1 response descriptor).

    If you want 3 result objects then you should use either:

    1. forceCollectionMapping on the mapping
    2. 3 response descriptors, each with the same mapping for items and each using a different keypath (rent / upcoming / watchnow).