Question

I'm trying to run a find by ID request on a REST API. I'm using RestKit 0.20. I have a Location object. It has an id attribute. I want to make a GET request to '/locations/:id' and receive the complete object as JSON. I have the backend and it's working. I'm now trying to write the iOS client code.

Here's what I have:

RKObjectManager* m = [RKObjectManager sharedManager];

RKObjectMapping* lmap = [RKObjectMapping requestMapping];
[lmap addAttributeMappingsFromArray:@[@"id"]];
RKRequestDescriptor* req = [RKRequestDescriptor requestDescriptorWithMapping:lmap objectClass:[Location class] rootKeyPath:nil];
[m addRequestDescriptor:req];

Location* l = [[Location alloc] init];
l.id = [NSNumber numberWithInt:177];
[m getObject:l path:@"locations/:id" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"LOADED: %@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"FAILED");
}];

After the code above is ran Restkit does not replace ':id: from the path with the ID attribute set in the Location object.

Do you guys have any ideas what I'm doing wrong?

UPDATE:

I had both request and response descriptors set for the Location class. I had a route added for the find_by_id request but it was a Named Route, not a Class Route. When I used the getObject:path:parameters:success:failure method the router did not fill in the 'id' placeholder (irregardless whether it was named 'id', 'object_id', 'identity' or whatever).

The solution I found is this:

  1. Continue using a Named Route but use the getObjectsAtPathForRouteNamed:object:parameters:success:failure method instead
  2. User a Class Route and continue using the getObject:path:parameters:success:failure method

The problem I was having was that when using a NamedRoute like so:

RKRoute * route = [RKRoute routeWithClass:className pathPattern:path method:RKRequestMethodFromString(method)];
[objectManager.router.routeSet addRoute:route];

and then querying for objects using the getObject:path:parameters:success:failure method did not cause the router to fill out any placeholders in the URL path.

Was it helpful?

Solution

You're using a request descriptor, but your aren't making a 'request' (PUT / POST). When doing a GET you need to use a response descriptor. Also, the mapping you're creating isn't specifying the class (so it's linked against NSDictionary. I'd usually use the response descriptor with a router too. Something like:

RKObjectManager* m = [RKObjectManager sharedManager];

RKObjectMapping* lmap = [RKObjectMapping mappingForClass:[Location class]];
[lmap addAttributeMappingsFromArray:@[@"identity"]];

RKResponseDescriptor* req = [RKResponseDescriptor responseDescriptorWithMapping:lmap pathPattern:@"locations/:identity" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
[m addResponseDescriptor:req];

[m.router.routeSet addRoute:[RKRoute routeWithClass:[Location class] pathPattern:@"locations/:identity" method:RKRequestMethodGET]];

Location* l = [[Location alloc] init];
l.identity = [NSNumber numberWithInt:177];

[m getObject:l path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"LOADED: %@", [mappingResult array]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"FAILED");
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top