最近业务需求需要用到信号量来控制block完成,再将结果放入数组中,然后进行for循环的下一个处理。
简单代码如下
dispatch_async(dispatch_get_global_queue(0,0), ^{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
for(NSIntegeri =0; i < count; i ++) {
dispatch_async(dispatch_get_global_queue(0,0), ^{
dispatch_semaphore_tsema2 =dispatch_semaphore_create(0);
for(NSIntegerj =0; j < count; j ++) {
[Task dosomething:^(NSString *data) {
addobject
dispatch_semaphore_signal(sema2);
}];
dispatch_semaphore_wait(sema2,DISPATCH_TIME_FOREVER);
}
NSDictionary*info =@{
};
addObject
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
todo2
});