Monitoring asynchronous connections with blocks

advertisements

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.

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.

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.

Can any confirm any of this?

__block int imageLocation = [allImages count] - 1;

  // 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 && 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"]];

         }];


Dealing with asynchronous methods is a pain ;)

In your case its guaranteed that the completion block will execute on the specified queue. However, you need to ensure that the queue has a max concurrent operations count of 1, otherwise concurrent access to shared resources is not safe. That's a classic race http://en.wikipedia.org/wiki/Race_condition. The max concurrent operations of a NSOperationQueue can be set with a property.

In general, completion handlers may execute on any thread, unless otherwise specified.

Dealing with asynchronous methods gets a lot easier when using a concept called "Promises" http://en.wikipedia.org/wiki/Promise_(programming). Basically, "Promises" represent a result that will be evaluated in the future - nonetheless the promise itself is immediately available. Similar concepts are named "futures" or "deferred".

There is an implementation of a promise in Objective-C on GitHub: RXPromise. When using it you also get safe access from within the handler blocks to shared resources. An implementation would look as follows:

-(RXPromise*) fetchImageFromURL:(NSString*)urlString queue:(NSOperationQueue*) queue
{
    @autoreleasepool {
        RXPromise* promise = [[RXPromise alloc] init];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
        [NSURLConnection sendAsynchronousRequest:request
                                           queue:queue
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                   if (data != nil) {
                                       [promise fulfillWithValue:data];
                                   }
                                   else { // There was an error
                                       [promise rejectWithReason:error];
                                   };
                               }];
        return promise;
    }
}

Then call it:

- (void) fetchImages {

    ...

    for (NSUInteger index = 0; index < N; ++index)
    {
        NSString* urlString = ...
        [self fetchImageFromURL:urlString, self.queue]
        .then(^id(id data){
            [self.allImages replaceObjectAtIndex:index withObject:[UIImage imageWithData:data]];
            return @"OK";
        },
        ^id(NSError* error) {
            [self.allImages replaceObjectAtIndex:index withObject:[UIImage imageNamed:@"error.png"]];
            return error;
        });
    }
}