iOS 应用因错误而超时而不是对其进行处理

iOS app times out on error instead of processing it

在 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];

当我让我的服务模拟错误而不是 returning 数据时,会发生以下情况。我的服务响应是

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: 的失败块不会立即执行。相反,该函数等待超时并且不会 return 一个错误对象。错误是 [已更新。替换为包括跟踪日志记录的日志]:

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 等待更多数据。这种行为的原因可能是什么?一些错误配置?服务响应格式是否错误?错误映射有问题吗?

在服务端,发送数据后连接没有关闭。

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