iOS RestKit响应描述符

时间:2015-09-06 19:31:06

标签: ios restkit

我想用RestKit创建通用响应描述符来捕获所有REST响应中的错误。我正在使用以下代码:

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];

[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];

RKResponseDescriptor *customErrorDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                 method:RKRequestMethodAny
                                            pathPattern:nil
                                                keyPath:@"error_message"
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptor:customErrorDescriptor];

这仅适用于响应,其中有另一个响应描述符(通常用于处理响应的GET方法):

RKResponseDescriptor *responseDescriptorGet =
    [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                 method:RKRequestMethodGET
                                            pathPattern:@"/api/user/user"
                                                keyPath:@"data"
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

但是对于没有自定义描述符的请求它不起作用。例如:

RKRequestDescriptor *requestDescriptor =
    [RKRequestDescriptor requestDescriptorWithMapping:[userMapping inverseMapping]
                                          objectClass:[User class]
                                          rootKeyPath:nil
                                               method:RKRequestMethodPUT];

它不会将响应映射到错误:

  

期望为“User”类型的对象映射,返回提供程序   一个'RKErrorMessage'

但是,当我添加另一个与此请求匹配的响应描述符并且响应中存在error_message时,通用错误描述符将成功匹配错误:

RKResponseDescriptor *responseDescriptorPut =
    [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                 method:RKRequestMethodPUT
                                            pathPattern:@"/api/user/updateCustomerProfile"
                                                keyPath:nil
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

添加此描述符的问题是,当发生错误时,它有时(约50%)“捕获”响应而不是错误描述符(因为两个描述符都符合要求)。

我尝试将bogus keyPath添加到此描述符中。它在响应发生错误时起作用,但在响应正常时它会给我警告:

  

在关键路径上找不到可映射的对象表示   搜索。,NSLocalizedFailureReason =映射操作无法执行   在搜索的关键路径中查找任何嵌套对象表示:   data,error_message输入到映射器的表示是   发现在以下键中包含嵌套对象表示   路径:结果这可能表示您配置错误   映射的关键路径。

0 个答案:

没有答案
相关问题