我只是将其作为练习:对每个 doSomethingAndReturn 使用 GCD 并发 Q 并使用串行 q 来监视回调的数量。当回调的数量等于 doSomethingAndReturn(s) 的数量时,则返回 prepareImages 数组。
我创建了代码来测试这个概念。
-(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");
// return asyncArray;
}
编辑:第二个想法:连续剧是没有必要的。