gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:12:31 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com