title: Delay when updating view in the completionHandler of an async HTTP request
body: <p>In my app when the user presses a button I start a HTTP asynchronous request (using <code>[NSURLConnection sendAsynchronousRequest...]</code>) and change the text of <code>UILabel</code> in the <code>completionHandler</code> block. This change, however, does not take place when the request is concluded and instead happens around 2-3 seconds later. Below is a code snippet that results in this behavior.</p>
  
  <pre><code>- (IBAction)requestStuff:(id)sender 
  {
      NSURL *url = [NSURL URLWithString:@"http://stackoverflow.com/"];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
  
      [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
       ^(NSURLResponse *response, NSData *data, NSError *error) 
       {
           NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;          
           exampleLabel.text = [NSString stringWithFormat:@"%d", httpResponse.statusCode];
       }];    
  }
  </code></pre>
  
  <p>A similar behavior happens when I attempt to create an <code>UIAlertView</code> inside the <code>completionHandler</code>.</p>
  
  <pre><code>- (IBAction)requestStuff:(id)sender 
  {
      NSURL *url = [NSURL URLWithString:@"http://stackoverflow.com/"];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
  
      [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
       ^(NSURLResponse *response, NSData *data, NSError *error) 
       {
           NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
  
           if ([httpResponse statusCode] == 200) {
               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"It worked!" 
                                                               message:nil
                                                              delegate:nil 
                                                     cancelButtonTitle:@"OK"
                                                     otherButtonTitles:nil];
               [alert show];
               [alert release];
           }
       }];    
  }
  </code></pre>
  
  <p>A small difference, though, is that the screen dims when <code>[alert show]</code> is executed. The alert itself only appears 2-3 seconds later like in the previous scenario.</p>
  
  <p>I'm guessing this is related to how the UI is handled by the app's threads, but I'm not sure. Any guidance on why the delay happens will be greatly appreciated.</p>