我正在使用RestKit 0.20.1将XML Feed映射到Core Data Model。具体如下:
<?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中我需要添加一个由玩家,季节和比赛的三个主键组成的identificationAttribute。竞争ID很容易映射,因为它直接位于keyPath payload.players.player.season.playerstats
(在playerStatsResponseDescriptor
中定义)之下。问题是我不知道如何从parent.id
引用parent.parent.id
和playerStatMapping
来分别映射到attFKSeasonID和attFKPlayerID。
我不认为RestKit支持访问entityMappings中的父keyPath,我相信可能有另一种解决方法吗?此图表显示了我想要提取的内容:
在伪代码中,如果我不使用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
}
}
}
所以这里基本上有两个问题:
player
时如何映射season
和playerStatsMapping
ID,以便playerStatsMapping
可以将它们用作identificationAttributes
?非常感谢任何帮助。
谢谢, Justyn
答案 0 :(得分:1)
简而言之,RestKit现在支持@parent
和@root
映射键作为development branch的一部分。这允许访问当前映射对象的父节点,如果它被映射为其父实体的关系。
这与我遇到的另一个问题非常相似,如果需要,我可以更详细地提供上述具体案例的答案,否则请查看this answer。