如何使用 NSURLConnection sendSynchronousRequest 更好地处理错误?有什么办法可以实现
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error
我有一个 nsoperation 队列,它在后台获取数据,这就是我有同步请求的原因。如果我必须实现异步请求,那么我该如何等待请求完成。因为该方法在没有数据的情况下无法继续进行。
如何使用 NSURLConnection sendSynchronousRequest 更好地处理错误?有什么办法可以实现
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error
我有一个 nsoperation 队列,它在后台获取数据,这就是我有同步请求的原因。如果我必须实现异步请求,那么我该如何等待请求完成。因为该方法在没有数据的情况下无法继续进行。
我不会依赖错误为非零来表明发生了错误。
我一直在使用 Ben 的回答中描述的错误检查,但我相信它在某些情况下会产生误报/虚假错误。
根据这个SO answer,我将更改为使用该方法的结果来确定成功/失败。然后(并且仅在那时)检查错误指针以获取失败的详细信息。
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
}
这种方法将避免对下载过程中发生的未导致失败的错误做出反应。我认为当下载发生时,它可能会遇到在框架内处理的非关键错误,并且它们具有使用错误对象设置错误指针的副作用。
例如,我收到了来自已下载应用程序的用户报告的 POSIX 错误,这些错误似乎在 URL 连接的处理过程中很深。
此代码用于报告错误:
NSString *errorIdentifier = [NSString stringWithFormat:@"(%@)[%d]",requestError.domain,requestError.code];
[FlurryAPI logError:errorIdentifier message:[requestError localizedDescription] exception:nil];
并且错误出现报告为:
Platform: iPhone
Error ID: (NSPOSIXErrorDomain)[22]
Msg: Operation could not be completed. Invalid argument
映射回来..
requestError.domain = NSPOSIXErrorDomain
requestError.code = 22
[requestError localizedDescription] = Operation could not be completed. Invalid argument
我所能挖掘的只是错误代码 22 是EINVAL
,但这并没有产生任何进一步的细节。
我得到的其他错误来自 NSURL 域,我完全期望在不同的网络条件下:
NSURLErrorTimedOut
NSURLErrorCannotConnectToHost
NSURLErrorNetworkConnectionLost
NSURLErrorNotConnectedToInternet
+others
-sendSynchronousRequest:returningResponse:error: 为您提供了一种在方法本身中获取错误的方法。最后一个参数确实是 (NSError **) 错误;也就是说,一个指向 NSError 指针的指针。试试这个:
NSError *error = nil;
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error];
if (error) {...handle the error}