很可能是一个相当微不足道的问题,但是完成块是否总是被调用[NSURLConnection sendAsynchronousRequest: ...]
?或者我必须实施超时计时器吗?
考虑以下我添加的地方MBProgressView
在调用之前并仅在完成块中将其删除:
[self showHUDWithTitle:@"Configuring"];
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error == nil) {
[self hideHUDWithFlag:YES
andTitle:@"Finished"
andSubtitle:@"(Box was configured)"];
} else if ([data length] == 0 && error == nil) {
[self hideHUDWithFlag:NO
andTitle:@"Failed"
andSubtitle:@"(Check box connection)"];
NSLog(@"Nothing was downloaded.");
} else if (error != nil) {
[self hideHUDWithFlag:NO
andTitle:@"Error"
andSubtitle:@"(Check box connection)"];
NSLog(@"Error = %@", error);
}
}];
是的,完成处理程序总是被调用。如果由于超时导致请求失败,error
将被设置并且data = nil
.
A NSURLRequest
默认超时为 60 秒,但您可以指定不同的值request.timeoutInverval
在开始连接之前。所以不需要额外的定时器。
Added:超时的情况下:
-
[error domain]
is NSURLErrorDomain
, and
-
[error code]
is NSURLErrorTimedOut
,
如果您只想显示错误消息,您可以使用[error localizedDescription]
,即“请求超时”。在这种情况下。 (这可能取决于区域设置。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)