实现事件同步 - 信号量的坑
有问题代码:
dispatch_async(self.queue_handleAppear, ^{
dispatch_semaphore_wait(self.semphore_t, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"hhh任务:%@ 开始执行",message);
[currentView makeToast:message duration:interval position:position withFinishBlock:^( BOOL completeRes) {
if (completeRes) {
NSLog(@"hhh任务:%@ 结束执行",message);
dispatch_semaphore_signal(self.semphore_t);
}
}];
});
});
注意: queue_handleAppear是串行队列
正确解决方式:
dispatch_async(self.queue_handleAppear, ^{
[self.condition lock];
dispatch_async(dispatch_get_main_queue(), ^{
[currentView makeToast:message duration:interval position:position withFinishBlock:^( BOOL completeRes) {
if (completeRes) {
[self.condition unlock];
}
}];
});
})