Will [NSURLConnection sendAsynchronousRequest: ...] always send completion block?
Asked Answered
H

3

5

Most likely a rather trivial question but will the completion block always be called using [NSURLConnection sendAsynchronousRequest: ...]? OR do I have to implement a timeout timer?

Consider the following where I add a MBProgressView before the call and remove it ONLY in the completion block:

[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);
     }
 }];
Hardboard answered 20/3, 2013 at 8:11 Comment(3)
naturally. a completion handler is a COMPLETION handler -- not a success handler :DGuardi
@Guardi I'm the success handler, not the completer that completes the completion!Hardboard
if(!error) = success :DGuardi
P
15

Yes, the completion handler is always called. If the request fails due to a timeout, the error will be set and data = nil.

A NSURLRequest has a default timeout of 60 seconds, but you can assign a different value to request.timeoutInverval before starting the connection. So there is no need for an extra timer.

Added: In the case of a timeout:

  • [error domain] is NSURLErrorDomain, and
  • [error code] is NSURLErrorTimedOut,

If you just want to present an error message, you can use [error localizedDescription], which is "The request timed out." in this case. (This may depend on the locale.)

Prospectus answered 20/3, 2013 at 8:23 Comment(1)
@Filip: You are welcome. I have added some information how you can check for a timeout.Prospectus
H
2
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                                              cachePolicy:NSURLCacheStorageAllowed             
                                          timeoutInterval:20];
Huberman answered 27/7, 2013 at 9:24 Comment(1)
Please try to explain what you did and why. People rarely learn from code-only answers.Henriques
B
0

[NSURLConnection sendAsynchronousRequest: ...] will definitely call a completion block in any scenario. However if you want to restrict this process to the max time, you can use the timeout timer as well.

For progress bar, how will you be incrementing the value? Instead of progress bar, I would suggest you to go with activity indicator.

Hope this helps.

Blighter answered 20/3, 2013 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.