I just do it as an exercise: use a GCD in parallel with Q for each doSomethingAndReturn and use serial q to control the number of callbacks. When the number of callbacks is equal to the number of doSomethingAndReturn (s), then we return the prepareImages array.
I created code to test the concept.
-(NSString *)doSomethingAndReturn1 { for (int i=0; i<30; i++) { NSLog(@"soSomethingAndReturn1 i: %i", i); } return @"soSomethingAndReturn1"; } -(NSString *)doSomethingAndReturn2 { for (int i=0; i<10; i++) { NSLog(@"soSomethingAndReturn2 i: %i", i); } return @"soSomethingAndReturn2"; } -(NSString *)doSomethingAndReturn3 { for (int i=0; i<20; i++) { NSLog(@"soSomethingAndReturn3 i: %i", i); } return @"soSomethingAndReturn3"; } -(void)addToArray:(NSString *)str { [asyncArray addObject:str]; NSLog(@"asyncArray: %@", asyncArray); } - (IBAction)buttonMultitasksPressed:(id)sender { dispatch_queue_t serialdQueue; serialdQueue = dispatch_queue_create("com.mydomain.testbed.multimethods", NULL); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self addToArray:[self doSomethingAndReturn1]]; }); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self addToArray:[self doSomethingAndReturn2]]; }); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self addToArray:[self doSomethingAndReturn3]]; }); dispatch_sync(serialdQueue, ^{ while (!([asyncArray count] == 3)) { NSLog(@"not there yet count: %i", [asyncArray count]); } }); NSLog(@"end of dispatch_sync serialQueue");
Edit: 2nd thought: The series is not needed.
source share