RestKit 0.20.1如何映射父ID

鉴于此XML有效负载:

                        

和映射:

 RKEntityMapping *monthlyReportMapping = [RKEntityMapping mappingForEntityForName:@"MonthlyReport" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]]; monthlyReportMapping.identificationAttributes = @[@"yearNumber", @"monthNumber"]]; [monthlyReportMapping addAttributeMappingsFromDictionary:@{ /* * How would I set up the mappings for the yearNumber * so I can use it as the composite identifier with * the monthNumber? I want to do something like this: */ @"@metadata.parent.yearNum" : @"yearNumber", @"monthNum" : @"monthNumber", @"desc" : @"description" }]; RKResponseDescriptor *monthlyMappingResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:monthlyReportMapping pathPattern:@"/monthlyReports" keyPath:@"payload.year.month" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [[RKObjectManager sharedManager] addResponseDescriptor:monthlyMappingResponseDescriptor]; 

当我在payload.year.month的keyPath中映射时,你如何从monthlyReportMapping访问yearNum?

请假设我无法控制XML响应。

谢谢Justyn

目前,通过元数据字典映射父ID的function不可用,但具有0.20.3版本里程碑的活动票证:

https://github.com/RestKit/RestKit/issues/1327

更新

RestKit的开发分支现在允许您使用@parent访问层次结构中的父节点或@root来访问层次结构中的根节点。

您正在遍历的层次结构基于您传递到responseDescriptor的keyPath。 因此,在上面的示例中,有两件事需要做。 首先创建一个与MonthlyReport实体具有to-many关系的新实体Year (记住连接反向 )。

现在映射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”键访问其parente节点。 映射月份报告数据时的层次结构如下:

 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即使我们当前正在映射月份对象。 该function还允许链接。 因此,如果你有更深的嵌套,你可以做@parent.@parent.@parent.attributeKey

这为RestKit增加了另一层灵活性!