pla*_*ker 1 iphone ios restkit restkit-0.20
我正在尝试在REST API上通过ID请求运行查找.我正在使用RestKit 0.20.我有一个Location对象.它有一个id属性.我想向'/ locations /:id'发出GET请求,并以JSON的形式接收完整的对象.我有后端,它正在工作.我现在正在尝试编写iOS客户端代码.
这就是我所拥有的:
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");
}];
Run Code Online (Sandbox Code Playgroud)
在运行上面的代码之后,Restkit不会从具有在Location对象中设置的ID属性的路径替换':id:.
你们有什么想法我做错了吗?
更新:
我为Location类设置了请求和响应描述符.我为find_by_id请求添加了一个路由,但它是一个命名路由,而不是一个类路由.当我使用getObject:path:参数:success:failure方法时,路由器没有填写'id'占位符(无论它是否被命名为'id','object_id','identity'或其他什么).
我找到的解决方案是这样的:
我遇到的问题是当使用NamedRoute时:
RKRoute * route = [RKRoute routeWithClass:className pathPattern:path method:RKRequestMethodFromString(method)];
[objectManager.router.routeSet addRoute:route];
Run Code Online (Sandbox Code Playgroud)
然后使用getObject:path:参数:success:failure方法查询对象不会导致路由器填写URL路径中的任何占位符.
您正在使用请求描述符,但您没有发出"请求"(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");
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2635 次 |
| 最近记录: |