Will [NSURLConnection sendAsynchronousRequest: ...] always send a completion block?

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

Consider the following when I add MBProgressViewbefore a call and delete 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);
     }
 }];
+5
source share
3 answers

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

A NSURLRequest 60 , request.timeoutInverval. , .

: :

  • [error domain] - NSURLErrorDomain
  • [error code] NSURLErrorTimedOut,

, [error localizedDescription], " ". . ( .)

+15
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                                              cachePolicy:NSURLCacheStorageAllowed             
                                          timeoutInterval:20];
+2

[NSURLConnection sendAsynchronousRequest:...] . , , -.

, ? .

, .

0

All Articles