Block variables in Objective-C
objective-c, objective-c-blocks
Solution
Look at the name of the method you are calling; `generateCGImagesAsynchronouslyForTimes: completionHandler:`.
Asynchronously means that it executes in a different thread (likely via a queue and, as @newaccount points, it may likely be re-scheduled for future execution on the current queue/thread) and the method returns immediately. Thus, when you set `f=count+1;`, the completion block hasn't even been executed yet because none of the image loads in background threads have been completed.
You need to make a call from that completion block back to your code that needs to respond to the completion. i.e.
^() {
....
dispatch_async(dispatch_get_main_queue(), ^{[self heyManAnImageLoadedDude];});
....
}
Problem
I have a questions about blocks in Objective-C. For example I have this code: ``` __block int count = 0; void (^someFunction)(void) = ^(void){ count = 4; }; count +=2; ``` What would be the proper way to write the same piece of code so the count will become 6, not 2 ?! Thank you! I should probably show the actual code because my previous question was blurry. EDIT: ``` __block CMTime lastTime = CMTimeMake(-1, 1); __block int count = 0; [_imageGenerator generateCGImagesAsynchronouslyForTimes:stops completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) { if (result == AVAssetImageGeneratorSucceeded) { NSImage *myImage = [[NSImage alloc] initWithCGImage:image size:(NSSize){50.0,50.0}]; [arrOfImages addObject:myImage]; } if (result == AVAssetImageGeneratorFailed) { NSLog(@"Failed with error: %@", [error localizedDescription]); } if (result == AVAssetImageGeneratorCancelled) { NSLog(@"Canceled"); } if (arrOfImages.count > 5) { NSLog(@"here"); } count++; }]; int f = count+1; ``` after 10 iterations count is 0...why?!?!