RestKit Router - How to implement routes with nested variables?

990 views Asked by At

In the past I have specified path parameters for every RKResponseDescriptor as well as every RKObjectManager get/post/patch/delete method. This works, but it seems like RKRouter is the better, more modular way to do this.

I'm having trouble setting up a dynamic route that contains nested variables. For example:

Adding a route

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

Setting up response descriptor

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

Making an object manager call

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

Before using a router, I would include the userID in the object manager call path. (Like: path:[NSString stringWithFormat:@"users/%@/locations/%@", [self getCurrentUser].userID, location.locationID]). However, when using a router, I can't seem to figure out how to specify that. What's the correct way to do this? I'm hoping the userID doesn't have to be hard coded into the route path.

It's important to note that all my mappings are set up properly, everything worked flawlessly before trying to implement routes. Any help is appreciated!

2

There are 2 answers

0
Whoa On BEST ANSWER

Keeping Wain's answer in mind, I ended up needing multiple different routes for a single object; the index of which is a relationship-based route. For example:

Index

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];

Update

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];
2
Wain On

Your route is fine, this is wrong:

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

Because you need to pass an instance of the Location class (not the class object) and that instance needs to have userID and locationID Attributes set so that they can be injected into the route path pattern.