过去,我为每个 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 不必硬编码到路由路径中。
重要的是要注意,我的所有映射都已正确设置,在尝试实施路线之前一切都完美无缺。任何帮助表示赞赏!