iOS应用程序在出错时超时而不是处理它

时间:2015-05-26 19:32:35

标签: ios restkit

在RestKit应用程序中,我正在快速地处理请求和响应,但现在我正在尝试添加错误处理。我跟随RestKit readme中的示例以及来自网络的类似示例,但遇到了一些奇怪的行为。

错误映射随

一起添加
// Error JSON looks like {"errors": "Some Error Has Occurred"}
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
// The entire value at the source key path containing the errors maps to the message
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError);
// Any response in the 4xx status code range with an "errors" key path uses this mapping
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"errors" statusCodes:statusCodes];

[objectManager addResponseDescriptor:errorDescriptor];

当我使我的服务模拟错误而不是返回数据时,会发生以下情况。我服务的回复是

HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 26 May 2015 19:01:21 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

{"errors":"This is a test exception"}

之后关闭连接。但是,在客户端,getObjectsAtPath:的失败块不会立即执行。相反,该函数等待超时并且不返回错误对象。错误是 [已更新。替换为日志,包括跟踪日志记录]

2015-05-27 14:08:25.968 Olive Oil[37455:37884261] I restkit:RKLog.m:49 RestKit logging initialized...
2015-05-27 14:08:26.275 Olive Oil[37455:37884261] 320.000000
2015-05-27 14:08:26.415 Olive Oil[37455:37884261] T restkit.network:RKObjectRequestOperation.m:148 GET 'http://192.168.69.88/OliveOil.svc/recipies':
request.headers={
    "Accept-Language" = "en;q=1";
    Authorization = "Basic XXXXXXXXXXXXX==";
    "User-Agent" = "Olive Oil/1 (iPad Simulator; iOS 8.3; Scale/2.00)";
}
request.body=(null)
2015-05-27 14:09:33.559 Olive Oil[37455:37885022] E restkit.network:RKObjectRequestOperation.m:544 Object request failed: Underlying HTTP request operation failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7f91f36ad440 {NSUnderlyingError=0x7f91f37271e0 "The request timed out.", NSErrorFailingURLStringKey=http://192.168.69.88/OliveOil.svc/recipies, NSErrorFailingURLKey=http://192.168.69.88/OliveOil.svc/recipies, NSLocalizedDescription=The request timed out.}
2015-05-27 14:09:33.559 Olive Oil[37455:37885022] E restkit.network:RKObjectRequestOperation.m:209 GET 'http://192.168.69.88/OliveOil.svc/recipies' (401 Unauthorized / 0 objects) [request=67.1418s mapping=0.0000s total=67.2429s]:error=Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7f91f36ad440 {NSUnderlyingError=0x7f91f37271e0 "The request timed out.", NSErrorFailingURLStringKey=http://192.168.69.88/OliveOil.svc/recipies, NSErrorFailingURLKey=http://192.168.69.88/OliveOil.svc/recipies, NSLocalizedDescription=The request timed out.}
2015-05-27 14:09:33.560 Olive Oil[37455:37885022] D restkit.network:RKObjectRequestOperation.m:210 response.body=(null)

显然,状态401被传达(第二个错误),但有些东西使得RestKit等待更多数据。可能是这种行为的原因是什么?有些配置错误?服务响应是否格式错误?错误映射有问题吗?

1 个答案:

答案 0 :(得分:0)

在服务方面,数据发送后连接未关闭。

进行适当的更改后,应用程序会按预期处理错误信息。谢谢,Wain,指出我正确的方向。