iphone - Restkit 不会将对象属性插入到请求 URL 路径的路径模式中

标签 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");
}];

运行上面的代码后,Restkit 不会替换路径中的 ':id: 和 Location 对象中设置的 ID 属性。

你们有什么想法我做错了什么吗?

更新:

我为 Location 类设置了请求和响应描述符。我为 find_by_id 请求添加了一条路由,但它是命名路由,而不是类路由。当我使用 getObject:path:parameters:success:failure 方法时,路由器没有填写“id”占位符(不管它是否被命名为“id”、“object_id”、“identity”或其他名称)。

我找到的解决方案是这样的:
  • 继续使用命名路由,但改用 getObjectsAtPathForRouteNamed:object:parameters:success:failure 方法
  • 使用类路由并继续使用 getObject:path:parameters:success:failure 方法

  • 我遇到的问题是,当使用这样的 NamedRoute 时:
    RKRoute * route = [RKRoute routeWithClass:className pathPattern:path method:RKRequestMethodFromString(method)];
    [objectManager.router.routeSet addRoute:route];
    

    然后使用 getObject:path:parameters: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");
    }];
    

    关于iphone - Restkit 不会将对象属性插入到请求 URL 路径的路径模式中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16308116/

    相关文章:

    iPhone SDK : MapKit multiple custom annotations

    iphone - SciFi-HiFi 钥匙串(keychain)问题 - SecKeychainItemRef 未声明错误

    添加 Default-568h@2x.png 后 iPhone 5 TabBar 不工作

    ios - AV播放器 : How to handle network interruptions

    ios - 为什么 NSJSONSerialization 在 iOS 6 上崩溃?

    iphone - UITableview 部分标题不显示文本

    iphone - 在 NSUserDefaults 中设置值时崩溃

    ios - 所有 ViewController 中的 AdMod 单个实例

    ios - 将 NSManagedObject 转换为子类返回 nil

    objective-c - RestKit 警告 : "Found a collection containing only NSNull values..."