RestKit 0.20.1 如何映射父 ID

Posted

技术标签:

【中文标题】RestKit 0.20.1 如何映射父 ID【英文标题】:RestKit 0.20.1 How to map parent id 【发布时间】:2013-06-04 12:34:50 【问题描述】:

鉴于此 XML 负载:

<payload>
        <year yearNum="2013">
                <month monthNum="6" desc="This month was an enlightening month"/>
                <month monthNum="5" desc="This month was a questioning month"/>
                <month monthNum="4" desc="This month was a good month"/>
                <month monthNum="3" desc="This month was a crazy month"/>
                <month monthNum="2" desc="This month was a dry month"/>
                <month monthNum="1" desc="This month was a slow month"/>
        </year>
        <year yearNum="2012">
                <month monthNum="12" desc="This month was a cold month"/>
                <month monthNum="11" desc="This month was an expensive month"/>
                <month monthNum="10" desc="This month was a free month"/>
                <month monthNum="9" desc="This month was a hard month"/>
                <month monthNum="8" desc="This month was a surprising month"/>
                <month monthNum="7" desc="This month was an energetic month"/>
                <month monthNum="6" desc="This month was a hasty month"/>
                <month monthNum="5" desc="This month was a relaxing month"/>
                <month monthNum="4" desc="This month was a fair month"/>
                <month monthNum="3" desc="This month was a strange month"/>
                <month monthNum="2" desc="This month was a lucky month"/>
                <month monthNum="1" desc="This month was a odd month"/>
        </year>
</payload>

和一个映射:

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 响应。

谢谢, 贾斯汀

【问题讨论】:

【参考方案1】:

目前通过元数据字典映射父 id 的功能不可用,但有一个 0.20.3 发布里程碑的活动票:

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

更新

RestKit 的development branch 现在允许您使用@parent 访问层次结构中的父节点或使用@root 访问层次结构中的根节点。

您正在遍历的层次结构基于您传递给您的 responseDescriptor 的 keyPath。所以在上面的例子中有两件事需要做。首先创建一个新实体Year,它与MonthlyReport实体有to-many关系(记得connect the inverse)。

现在如下映射 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.@parent.@parent.attributeKey

这为 RestKit 增加了另一个级别的灵活性!

【讨论】:

好回答的人..为了完整起见,您能否提供月/年的实体字段/关系?

以上是关于RestKit 0.20.1 如何映射父 ID的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 0.20.1 将本地 JSON 映射到 Core Data 崩溃

将 JSON 键值映射到 RestKit 0.20.1 中的核心数据属性

Restkit/Core Data 关系映射,实体到相同类型的实体(父/子)

RestKit 0.20.1 复合键和嵌套关系

Restkit:将请求中的字段包含到对象映射中

参考 id 的 RestKit 映射对象