使用NSURLConnection sendSynchronousRequest进行错误处理

Nnp*_*Nnp 19 iphone error-handling nsurlconnection

如何使用NSURLConnection sendSynchronousRequest进行更好的错误处理?有什么方法可以实现

- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)

我有一个nsoperation队列,它在后台获取数据,这就是为什么我有同步请求.如果我必须实现异步请求,那么我该如何等待请求完成.因为没有数据,该方法无法进行.

ohh*_*rob 55

我不会依赖错误是非nil来表示发生了错误.

我一直在使用Ben的答案中描述的错误检查,但我相信它在某些情况下会产生误报/虚假错误.

根据这个SO答案,我正在改变使用方法的结果来确定成功/失败.然后(并且只有那时)检查错误指针以获取故障的详细信息.

NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
/* Return Value
   The downloaded data for the URL request. Returns nil if a connection could not be created or if the download fails.
*/
if (response == nil) {
    // Check for problems
    if (requestError != nil) {
        ...
    }
}
else {
    // Data was received.. continue processing
}
Run Code Online (Sandbox Code Playgroud)

此方法将避免对下载过程中发生的错误做出反应,该错误不会导致失败.我认为随着下载的发生,它可能会遇到在框架内处理的非关键错误,并且它们具有使用错误对象设置错误指针的副作用.

例如,我一直在收到已下载应用程序的用户报告的POSIX错误,这些用户似乎已经在URL连接的处理过程中发生过.

此代码用于报告错误:

NSString *errorIdentifier = [NSString stringWithFormat:@"(%@)[%d]",requestError.domain,requestError.code];
[FlurryAPI logError:errorIdentifier message:[requestError localizedDescription] exception:nil];
Run Code Online (Sandbox Code Playgroud)

并且错误显示为:

Platform: iPhone
Error ID: (NSPOSIXErrorDomain)[22]
Msg: Operation could not be completed. Invalid argument
Run Code Online (Sandbox Code Playgroud)

映射回来..

requestError.domain = NSPOSIXErrorDomain
requestError.code = 22
[requestError localizedDescription] = Operation could not be completed. Invalid argument
Run Code Online (Sandbox Code Playgroud)

所有我已经能够挖掘,是错误代码22 EINVAL,但这还没有产生任何进一步的细节.

我得到的其他错误来自NSURL域,我完全期望在不同的网络条件下:

NSURLErrorTimedOut
NSURLErrorCannotConnectToHost
NSURLErrorNetworkConnectionLost
NSURLErrorNotConnectedToInternet
+others
Run Code Online (Sandbox Code Playgroud)


Ben*_*ieb 29

-sendSynchronousRequest:returningResponse:error:为您提供了一种在方法本身中获取错误的方法.最后一个参数确实是(NSError**)错误; 也就是说,指向NSError指针的指针.试试这个:

NSError        *error = nil;
NSURLResponse  *response = nil;

[NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error];

if (error) {...handle the error}
Run Code Online (Sandbox Code Playgroud)

  • 在测试错误之前,您确实需要测试结果.从文档*"如果无法创建连接或下载失败,则返回nil"*http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference html的#// apple_ref/OCC/CLM/NSURLConnection的/ sendSynchronousRequest:returningResponse:错误: (10认同)
  • 确保初始化错误和响应为nil,我得到EXC_BAD_ACCESS错误,直到我找到这个答案.谢谢+1! (8认同)
  • 将错误初始化为nil是错误的; 它不是必需的.测试错误而不是返回值也是错误的. (7认同)
  • Cocoa [担保](http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreomizeCnomizeNSError/CreateCustomizeNSError.html#//apple_ref/doc/uid/TP40001806-CH204-SW1)如果方法失败,错误对象将有效; 它_ notes not_保证如果方法成功,错误对象将为`nil`.在使用错误之前,您必须检查直接返回值. (5认同)