1

我正在使用 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>

核心数据模型

核心数据模型

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

实体映射

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 中我需要添加一个由 Player、Season 和 Competition 三个主键组成的标识属性。比赛 id 很容易映射,因为它直接在 keyPath 下payload.players.player.season.playerstats(在 中定义playerStatsResponseDescriptor)。问题是我不知道如何引用parent.idparent.parent.idplayerStatMapping映射到 attFKSeasonID 和 attFKPlayerID 分别。

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

XML 映射

在伪代码中,如果我不使用 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. 构建时如何映射playerseasonidplayerStatsMapping以便playerStatsMapping可以将它们用作identificationAttributes

任何帮助将不胜感激。

谢谢,贾斯汀

4

1 回答 1

1

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

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

于 2013-06-11T15:18:49.933 回答