Given this XML payload:
Currently the feature of mapping the parent id via the metadata dictionary is not available but has an active ticket for the 0.20.3 release milestone:
https://github.com/RestKit/RestKit/issues/1327
The development branch of RestKit now lets you use @parent
to access the parent node in the hierarchy or @root
to access the root node in the hierarchy.
The hierarchy you are traversing up is based on the keyPath you passed into your responseDescriptor. So in the example above there are two things that need doing. Firstly create a new entity Year
that has a to-many
relationship with the MonthlyReport
entity (remember to connect the inverse).
Now map the XML payload as follows:
RKEntityMapping *yearMapping =
[RKEntityMapping mappingForEntityForName:@"Year"
inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
yearMapping.identificationAttributes = @[@"yearNumber"]];
[yearMapping addAttributeMappingsFromDictionary:@{
@"yearNum" : @"yearNumber"
}];
RKEntityMapping *monthlyReportMapping =
[RKEntityMapping mappingForEntityForName:@"MonthlyReport"
inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
monthlyReportMapping.identificationAttributes = @[@"monthYearNumber", @"monthNumber"]];
[monthlyReportMapping addAttributeMappingsFromDictionary:@{
@"@parent.yearNum" : @"monthYearNumber",
@"monthNum" : @"monthNumber",
@"desc" : @"monthDescription"
}];
// Map the keyPath of `month` to our coredata entity
// relationship `months` using our monthReportMapping
[yearMapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"month"
toKeyPath:@"months"
withMapping:monthlyReportMapping]];
// Notice how the keyPath now points to payload.year
RKResponseDescriptor *monthlyReportMappingResponseDescriptor
= [RKResponseDescriptor responseDescriptorWithMapping:yearMapping
pathPattern:@"/monthlyReports"
keyPath:@"payload.year"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager]
addResponseDescriptor:monthlyReportMappingResponseDescriptor];
When we then call:
[[RKObjectManager sharedManager]
getObjectsAtPath:@"/monthlyReports" parameters:nil success:nil failure:nil];
this will map the year data onto our Year
entity and in turn then map the month data to our MonthlyReport
entity. As the month data gets mapped, it has access to its parente nodes via the `@parent' key. The hierarchy at the time of mapping the month report data is this:
yearNum: @2013
[
month { // <-- Currently mapping the month.
// We used to only get to see what was inside
// this with no access to the parent nodes.
monthNum: @6,
desc: @"This month was an enlightening month"
},
month {
monthNum: @5,
desc: @"This month was a questioning month"
},
…
];
@parent.yearNum
allows us to access the yearNum
even though we are currently mapping a month object. The functionality also allows chaining. So if you had deeper nesting, you could do @parent.@parent.@parent.attributeKey
.
This adds yet another level of flexibility to RestKit!