title: Tracking Asynchronous Connections with Blocks
body: <p>I am doing a lot of URL requests (about 60 small images) and I have started to do them Asynchronously.  My code adds another image (little downloading thing) and then sets a Request going.</p>
  
  <p>When the request is done I want "data" to be put in the location which was originally added for it, however, I can not see how to pass "imageLocation" to the block for it to store the image in the correct location.</p>
  
  <p>I have replaced the 3rd line with below which seems to work but I am not 100% it is correct (it is very hard to tell as the images are nearly identical).  I am also thinking that it is possible to pass "imageLocation" at the point where the block is declared.</p>
  
  <p>Can any confirm any of this?</p>
  
  <blockquote>
    <p>__block int imageLocation = [allImages count] - 1;</p>
  </blockquote>
  
  <pre><code>  // Add another image to MArray
    [allImages addObject:[UIImage imageNamed:@"downloading.png"]];
    imageLocation = [allImages count] - 1;
  
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
              [request setTimeoutInterval: 10.0];
              [NSURLConnection sendAsynchronousRequest:request
              queue:[NSOperationQueue currentQueue]
              completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  
                       if (data != nil &amp;&amp; error == nil)
                       {
                           //All Worked
                           [allImages replaceObjectAtIndex:imageLocation withObject:[UIImage imageWithData:data]];
  
                       }
                       else
                       {
                           // There was an error, alert the user
                           [allImages replaceObjectAtIndex:imageLocation withObject:[UIImage imageNamed:@"error.png"]];
  
           }];
  </code></pre>