1

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.

4

1 回答 1

5

您正在使用请求描述符,但您没有发出“请求”(PUT / POST)。执行 GET 时,您需要使用响应描述符。此外,您正在创建的映射没有指定类(因此它与 . 链接NSDictionary。我通常也会将响应描述符与路由器一起使用。类似于:

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");
}];
于 2013-05-01T08:06:23.577 回答