ios - RestKit 0.20.1 复合键和嵌套关系

标签 ios xml core-data restkit restkit-0.20

我正在使用 RestKit 0.20.1 将 XML 提要映射到核心数据模型。这些如下:

XML 提要

<?xml version="1.0" encoding="utf-8"?>
<payload>
    <competitions>
        <competition id="100" name="Comp A" />
        <competition id="200" name="Comp B" />
        <competition id="300" name="Comp C" />
        <competition id="400" name="Comp D" />
    </competitions>
    <seasons>
        <season id="10" span="2008/09"/>
        <season id="20" span="2009/10"/>
        <season id="30" span="2010/11"/>
        <season id="40" span="2011/12"/>
        <season id="50" span="2012/13"/>
    </seasons>
    <players>
        <player id="1" name="Justyn Spooner">
            <season id="10">
                <playerstats competitionID="100" goals="0" played="0" />
                <playerstats competitionID="200" goals="5" played="4" />
                <playerstats competitionID="300" goals="2" played="1" />
            </season>
            <season id="20">
                <playerstats competitionID="300" goals="1" played="4" />
                <playerstats competitionID="400" goals="2" played="9" />
            </season>
        </player>
    </players>
</payload>

核心数据模型

Core Data Model

到目前为止,我已经设置了实体映射并配置了响应描述符,但不确定如何设置它们之间的关系。

实体映射

RKEntityMapping *playerMapping = [RKEntityMapping mappingForEntityForName:@"Player" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
playerMapping.identificationAttributes = @[@"attID"];
[playerMapping addAttributeMappingsFromDictionary:@{
    @"id"   : @"attID",
    @"name" : @"attName"
}];

RKEntityMapping *competitionMapping = [RKEntityMapping mappingForEntityForName:@"Competition" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
competitionMapping.identificationAttributes = @[@"attID"];
[competitionMapping addAttributeMappingsFromDictionary:@{
    @"id"   : @"attID",
    @"name" : @"attName"
}];

RKEntityMapping *seasonMapping = [RKEntityMapping mappingForEntityForName:@"Season" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
seasonMapping.identificationAttributes = @[@"attID"];
[seasonMapping addAttributeMappingsFromDictionary:@{
    @"id"   : @"attID",
    @"span" : @"attYearSpan"
}];

RKEntityMapping *playerStatsMapping = [RKEntityMapping mappingForEntityForName:@"PlayerStats" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
playerStatsMapping.identificationAttributes = @[@"attFKCompetitionID", @"attFKSeasonID", @"attFKPlayerID"];
[playerStatsMapping addAttributeMappingsFromDictionary:@{
    @"competitionID" : @"attFKCompetitionID",
    @"????"          : @"attFKSeasonID",
    @"????"          : @"attFKPlayerID",
    @"goals"         : @"attGoals",
    @"played"        : @"attGamesPlayed"
}];

响应描述符

RKResponseDescriptor *playerResponseDescriptor =        [RKResponseDescriptor responseDescriptorWithMapping:playerMapping       pathPattern:kURLBasePath@"/players" keyPath:@"payload.players.player"                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor *seasonResponseDescriptor =        [RKResponseDescriptor responseDescriptorWithMapping:seasonMapping       pathPattern:kURLBasePath@"/players" keyPath:@"payload.seasons.season"                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor *competitionResponseDescriptor =   [RKResponseDescriptor responseDescriptorWithMapping:competitionMapping  pathPattern:kURLBasePath@"/players" keyPath:@"payload.competitions.competition"             statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor *playerStatsResponseDescriptor =   [RKResponseDescriptor responseDescriptorWithMapping:playerStatsMapping  pathPattern:kURLBasePath@"/players" keyPath:@"payload.players.player.season.playerstats"    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

除了缺少的关系之外,您会注意到在 playerStatsMapping 中我需要添加一个 identificationAttribute,它由 Player、Season 和 Competition 三个主键组成。比赛 ID 很容易映射,因为它直接位于 keyPath payload.players.player.season.playerstats 下(在 playerStatsResponseDescriptor 中定义)。问题是我不知道如何从 playerStatMapping 中引用 parent.idparent.parent.id 来映射到分别为 attFKSeasonID 和 attFKPlayerID。

我认为 RestKit 不支持访问 entityMappings 中的父 keyPath,我确定可能有另一种解决方法?这张图显示了我想要提取的内容:

XML Mapping

在伪代码中,如果我不使用 RestKit,这就是我想要做的:

For each player in the XML response:
{
    find out if that player exists in Core Data; create if not
    keep it around in a local variable
    for each season under the player in the XML:
    {
        create if needed; keep around.
        for each competition under the season in the XML:
        {
            create if needed; keep around.
            Take the current player, season and competition. 
            Is there a PlayerStat matching those three things? Create if not
            Set that player stat's attGoals and attGamesPlayed to the values that are mapped from goals and played in the XML
        }
    }
}

所以这里基本上有两个问题:

  1. 您将如何设置关系映射?
  2. 在构造 playerStatsMapping 时如何映射 playerseason id,以便 playerStatsMapping 可以使用它们作为 identificationAttributes?

如有任何帮助,我们将不胜感激。

谢谢, 贾斯汀

最佳答案

简而言之,RestKit 现在支持 @parent@root 映射键作为 development branch 的一部分.这允许访问当前映射对象的父节点,如果它被映射为其父实体的关系的话。

这与我遇到的另一个问题非常相似,如果需要,我可以更详细地提供上述具体案例的答案,否则请查看this answer .

关于ios - RestKit 0.20.1 复合键和嵌套关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16858200/

相关文章:

ios - 如何保留使用 AFNetworking 检索的 JSON 数据的顺序?

android - React-native 获取通话记录(Android/iOS)

xml - 通过属性值选择 XML 元素并添加一个元素

iOS Core Data 对多关系插入/获取

swift - NSBatchDeleteRequest 与 NSPredicate swift

ios - Swift - 从 map 注释执行 Segue

ios - 不同类型的 Json 属性

具有不同语言的所有国家/地区的 Android 字符串数组

android - 在 fragment 容器上调用 FragmentTransaction.replace() 时会发生什么?

ios - 使用 NSThread 和核心数据时遇到问题,更改未正确写入