0

过去,我为每个 RKResponseDescriptor 以及每个 RKObjectManager get/post/patch/delete 方法指定了路径参数。这可行,但似乎 RKRouter 是更好、更模块化的方法。

我无法设置包含嵌套变量的动态路由。例如:

添加路线

RKRoute *locationsRoute = [RKRoute routeWithClass:[Location class]     pathPattern:@"users/:userID/locations/:locationID" method:RKRequestMethodAny];
[[RKObjectManager sharedManager].router.routeSet addRoutes:@[locationsRoute]];

设置响应描述符

 RKResponseDescriptor *locationResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:locationResponseMapping
                                                                                          method:RKRequestMethodAny
                                                                                     pathPattern:nil
                                                                                         keyPath:nil
                                                                                     statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

进行对象管理器调用

[[RKObjectManager sharedManager] getObject:[Location class] path:nil parameters:nil success:nil failure:nil];

在使用路由器之前,我会在对象管理器调用路径中包含用户 ID。(如:)path:[NSString stringWithFormat:@"users/%@/locations/%@", [self getCurrentUser].userID, location.locationID]。但是,在使用路由器时,我似乎无法弄清楚如何指定它。这样做的正确方法是什么?我希望用户 ID 不必硬编码到路由路径中。

重要的是要注意,我的所有映射都已正确设置,在尝试实施路线之前一切都完美无缺。任何帮助表示赞赏!

4

2 回答 2

1

你的路线很好,这是错误的:

[[RKObjectManager sharedManager] getObject:[Location class] ...

因为您需要传递类的实例Location(而不是类对象),并且该实例需要设置userIDlocationID属性,以便它们可以注入到路由路径模式中。

于 2014-01-14T18:50:12.250 回答
0

牢记 Wain 的回答,我最终需要为单个对象提供多条不同的路线;其索引是基于关系的路由。例如:

指数

RKRoute *locationIndexRoute = [RKRoute routeWithRelationshipName:@"locations" objectClass:[User class] pathPattern:@"users/:userID/locations" method:RKRequestMethodGET];

[[RKObjectManager sharedManager] getObjectsAtPathForRelationship:@"locations" ofObject:[self getCurrentUser] parameters:nil success:nil failure:nil];

更新

RKRoute *locationUpdateRoute = [RKRoute routeWithClass:[Location class] pathPattern:@"users/:userID/locations/:locationID" method:RKRequestMethodPATCH];

[[RKObjectManager sharedManager] patchObject:location path:nil parameters:nil success:nil failure:nil];
于 2014-01-14T21:33:08.650 回答