【问题标题】:RestKit multiple response descriptors for the same pathPattern相同 pathPattern 的 RestKit 多个响应描述符
【发布时间】:2014-08-20 14:28:51
【问题描述】:

有没有办法定义 2 个或多个响应描述符都具有相同的 pathPattern?我的假设是,它们不仅会根据pathPattern 匹配,还会根据method 匹配。但显然情况并非如此。

这是我的对象映射:

RKObjectMapping *commentMapping = [RKObjectMapping mappingForClass:[Comment class]];
[commentMapping addAttributeMappingsFromArray:@[@"comment", @"rating", @"views", @"venue", @"user_views"]];

当我发出 GET 请求时,我会得到一个类似这样的列表:

{
    'meta': {
        'total_objects': 2,
        'limit': 20,
        ...
    },
    'objects': [
         {
             'comment': '...',
             'rating': 3.5,
             ...
         },
         ...
    ]
}

当我发出一个 POST 请求来创建一个新对象时,我得到的结果是这样的:

{
    'comment': '...',
    'rating': 3.5,
    ...
}

所以我想做的是定义两个不同的responseDescriptors——一个用于每个请求方法。所以这就是我所做的:

[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:commentMapping
                                                                                  method:RKRequestMethodGET
                                                                             pathPattern:@"comment/"
                                                                                 keyPath:@"objects"
                                                                             statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:commentMapping
                                                                                  method:RKRequestMethodPOST
                                                                             pathPattern:@"comment/"
                                                                                 keyPath:nil
                                                                             statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

问题是,总是使用第二个(带有nil keyPath 的那个)。即使对于 GET 请求。这真的把事情搞砸了。

我做错了什么?或者有什么办法可以在这里做我想做的事吗?

【问题讨论】:

  • 你好,我的情况和你一样。你想好怎么解决了吗?

标签: ios objective-c restkit


【解决方案1】:

RKObjectManager 有一个名为 *addResponseDescriptorsFromArray:(NSArray ) 的方法。您需要将两个响应描述符添加到 NSArray,然后将 NSArray 传递给此方法。

你可以在这里找到更多关于该方法的信息http://restkit.org/api/latest/Classes/RKObjectManager.html#//api/name/addRequestDescriptorsFromArray:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2017-08-23
    • 2013-05-16
    • 2014-06-04
    • 1970-01-01
    相关资源
    最近更新 更多