Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios [NSURLConnection sendAsynchronousRequest:…]是否始终发送完成块?_Ios_Objective C_Cocoa Touch_Nsurlconnectiondelegate - Fatal编程技术网

Ios [NSURLConnection sendAsynchronousRequest:…]是否始终发送完成块?

Ios [NSURLConnection sendAsynchronousRequest:…]是否始终发送完成块?,ios,objective-c,cocoa-touch,nsurlconnectiondelegate,Ios,Objective C,Cocoa Touch,Nsurlconnectiondelegate,最有可能是一个相当琐碎的问题,但是否总是使用[NSURLConnection sendAsynchronousRequest:…]调用完成块?还是我必须实现一个超时计时器 考虑以下情况,在调用之前添加一个MBProgressView,并仅在完成块中删除它: [self showHUDWithTitle:@"Configuring"]; [NSURLConnection sendAsynchronousRequest:request

最有可能是一个相当琐碎的问题,但是否总是使用
[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);
     }
 }];

[NSURLConnection sendAsynchronousRequest:…]在任何情况下都肯定会调用完成块。但是,如果要将此过程限制为最长时间,也可以使用超时计时器

对于进度条,您将如何增加该值?我建议您使用活动指示器,而不是进度条


希望这有帮助。

是的,始终调用完成处理程序。如果请求因超时而失败,将设置
错误
,并且
data=nil

NSURLRequest
的默认超时为60秒,但在开始连接之前,您可以为
request.timeoutiverval
指定不同的值。因此不需要额外的计时器

添加:在超时的情况下:

  • [错误域]
    NSURLErrorDomain
    ,并且
  • [错误代码]
    NSURLErrorTimedOut

如果您只想显示错误消息,可以使用
[error localizedDescription]
,在本例中为“请求超时”。(这可能取决于区域设置。)

@Filip:不客气。我已经添加了一些关于如何检查超时的信息。完成处理程序是完成处理程序,而不是成功处理程序:D@Daij-Djan我是成功处理者,而不是完成完成的完成者!如果(!error)=成功:请尝试解释您所做的事情以及原因。人们很少只从代码答案中学习。
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                                              cachePolicy:NSURLCacheStorageAllowed             
                                          timeoutInterval:20];