```
//dispatch_semaphore需要在异步线程中使用
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//初始化信号量为0,当信号量小于0时,线程会进入等待状态
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
for(NSIntegeri =0; i<10; i++){
//开始执行异步操作
[http uploadDataWithCompleteBlock:^(id _NullableaResponseObject, NSError *_NullableanError) {
NSLog(@"++第%ld次",i);
//异步结束之后,信号量加1,线程回复执行
dispatch_semaphore_signal(sem);
}];
//立即进入等待状态,信号量减一
dispatch_semaphore_wait(sem,DISPATCH_TIME_FOREVER);
}
});
```
网友评论