Hi all,
I've looked at some of the presentations form WWDC 2010 and also read most of the documents on blocks and concurrency and have a couple of questions regarding using blocks with serial queues in Grand Central Dispatch. I have an iOS 4 project that has a scrollview and a dictionary of image information - urls to the images and so on. I want to use GCD and blocks to download the images and put them in my scrollview thus not blocking the main thread. I have writen the following code which seems to work:
for (NSDictionary* dict in images)
{
dispatch_async(image_queue, ^{
NSString* urlString = [dict objectForKey:@"url"];
NSURL* url = [NSURL URLWithString:urlString];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:imageData];
UIImageView* imageView = // initialize imageView with image;
dispatch_async(dispatch_get_main_queue(), ^{
[self.scrollView addSubview:imageView];
});
[imageData release];
});
}
I have two questions:
According to the concurrency guide I should not capture variables from the enclosing scope that are non scalar types - in my code I capture dict which is an NSDictionary* object. If I am not allowed to capture it, how should I then write the code? Does a block only capture variables from the enclosing scope that are actually used?
What happens if I leave the current ViewController before all the images are fetched through the serial dispatch queue? I don't think that they are aware that the ViewController that created them is gone so what happens when they execute the completion handler where I insert the image views into my scrollview on the main thread? Does it cause an error or what? And how can I cancel any remaining operations on the serial queue when my ViewController disappears?
Best regards,