目前,通过元数据字典映射父 ID 的功能不可用,但有 0.20.3 版本里程碑的有效票证:
https://github.com/RestKit/RestKit/issues/1327
Update
The 开发分支RestKit 现在可以让你使用@parent
访问层次结构中的父节点或@root
访问层次结构中的根节点。
您要遍历的层次结构基于您传递到responseDescriptor 中的keyPath。所以在上面的例子中,有两件事需要做。首先创建一个新实体Year
有一个to-many
与的关系MonthlyReport
实体(记住连接逆).
现在映射 XML 有效负载,如下所示:
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];
当我们调用时:
[[RKObjectManager sharedManager]
getObjectsAtPath:@"/monthlyReports" parameters:nil success:nil failure:nil];
这会将年份数据映射到我们的Year
实体,然后将月份数据映射到我们的MonthlyReport
实体。当月份数据被映射时,它可以通过“@parent”键访问其父节点。映射月报表数据时的层次结构是这样的:
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
允许我们访问yearNum
即使我们当前正在映射一个月对象。该功能还允许链接。所以如果你有更深的嵌套,你可以这样做@parent.@[email protected]
.
这为 RestKit 增加了另一个级别的灵活性!