Ios RestKit-发布或放置包含嵌套实体的实体
我有一个iOS应用程序,它使用Ios RestKit-发布或放置包含嵌套实体的实体,ios,ruby-on-rails,core-data,nested,restkit,Ios,Ruby On Rails,Core Data,Nested,Restkit,我有一个iOS应用程序,它使用RestKit在我的核心数据模型和Rails API之间进行同步 我的核心数据模型中有游戏和团队实体。一场比赛与多支球队有着千丝万缕的联系。我正在尝试更新团队的“score”属性,然后通过在游戏中发送来尝试在我的RKObjectManager上运行putObject方法。服务器上没有更新团队的分数 如果我更改了游戏的属性,例如“状态”,然后使用putObject发送到游戏中,它将正常工作 如果对象中有嵌套对象,是否可以使用putObject更新多个对象?或者,当我更
RestKit
在我的核心数据模型和Rails API之间进行同步
我的核心数据模型中有游戏和团队实体。一场比赛与多支球队有着千丝万缕的联系。我正在尝试更新团队的“score”属性,然后通过在游戏中发送来尝试在我的RKObjectManager
上运行putObject方法。服务器上没有更新团队的分数
如果我更改了游戏的属性,例如“状态”,然后使用putObject发送到游戏中,它将正常工作
如果对象中有嵌套对象,是否可以使用putObject更新多个对象?或者,当我更新团队的“分数”属性时,是否需要在团队中运行putObject
这是我的游戏映射代码
Class itemClass = [Game class];
RKEntityMapping *mapping = [RKEntityMapping
mappingForEntityForName:@"Game"
inManagedObjectStore:managedObjectStore];
mapping.identificationAttributes = @[@"gameID"];
NSDictionary *standardDict = @{@"id": @"gameID",
@"created_at": @"createdAt",
@"updated_at": @"updatedAt"};
NSDictionary *gameDict = @{@"league_id": @"leagueID",
@"location_id": @"locationID",
@"state": @"state",
//.... more attributes....
};
[mapping addAttributeMappingsFromDictionary:standardDict];
[mapping addAttributeMappingsFromDictionary:gameDict];
[mapping addConnectionForRelationship:@"league" connectedBy:@{@"leagueID": @"leagueID"}];
[mapping addConnectionForRelationship:@"location" connectedBy:@{@"locationID": @"locationID"}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
NSString *keyPath = nil;
NSString *itemsPath = @"games/:gameID";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodAny
pathPattern:itemsPath
keyPath:keyPath
statusCodes:statusCodes];
[manager addResponseDescriptor:responseDescriptor];
NSString *itemPath = @"game";
RKEntityMapping *requestMapping = [mapping inverseMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:itemClass
rootKeyPath:itemPath
method:RKRequestMethodAny];
[manager addRequestDescriptor:requestDescriptor];
//route for manipulating with existing object
RKRoute *itemRoute = [RKRoute routeWithClass:itemClass pathPattern:@"games/:gameID" method:RKRequestMethodAny];
itemRoute.shouldEscapePath = YES;
[manager.router.routeSet addRoutes:@[itemRoute]];
团队的映射基本上是以完全相同的方式编写的,除了团队基于游戏的“gameID”连接到游戏之外。因此-->[mapping addConnectionForRelationship:@“Game”connectedBy:@{“gameID”:@“gameID”}]代码>您正在响应映射上使用外键映射:
[mapping addConnectionForRelationship:@"league" connectedBy:@{@"leagueID": @"leagueID"}];
[mapping addConnectionForRelationship:@"location" connectedBy:@{@"locationID": @"locationID"}];
当您使用反向映射时,它们不会反转(因为它们没有包含足够的信息来创建反向)
因此,您的requestMapping
需要显式更新,以包含一个关系映射,告诉RestKit处理关系以及如何将关系内容映射到生成的JSON中。那么您是说我的requestMapping
需要更新以包含“reversed”吗联盟
和位置
的关系?或者您是说我的请求映射需要更新,以便在我的游戏中包含与团队的关系?如果是后者,那会是什么样子?我可以想象类似于[requestMapping addConnectionForRelationship:@“teams”connectedBy:@{@“?”:@“?”}
但是,connectedBy
参数是什么?无论您想要发送哪个关系。您不能使用addConnectionForRelationship
,请使用addRelationship
我在requestMapping
上看到的唯一可以使用的方法是addRelationshippingwithsourceKeyPath:mapping:
。。。这就是我要寻找的吗?是的(它使用映射,因此可以直接应用,不像外键连接),对于响应描述符,源是JSON中的键,对于请求,源是数据模型(团队)中的键。目标键是要放入JSON的内容。映射是如何处理关系内容。