RestKit 0.20.1 How to map parent id

China☆狼群 提交于 2019-12-19 10:56:11

问题


Given this XML payload:

<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>

and a mapping of:

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];

How would you get access to the yearNum from within the monthlyReportMapping when I'm mapping within the keyPath of payload.year.month?

Please assume that I have no control over the XML response.

Thanks, Justyn


回答1:


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

Update

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!



来源:https://stackoverflow.com/questions/16918002/restkit-0-20-1-how-to-map-parent-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!