您如何确定是否dispatch_semaphore_t
正在等待是否导致等待?我最初是在想:
if ( dispatch_semaphore_wait(mySemaphore, DISPATCH_TIME_NOW) ) {
NSLog(@"No more resources, wait");
} else {
NSLog(@"Resources available, shouldn't wait");
}
但是做dispatch_semaphore_wait()
信号量的行为减少了,所以我在想:
if ( dispatch_semaphore_wait(mySemaphore, DISPATCH_TIME_NOW) ) {
NSLog(@"No more resources, wait");
} else {
dispatch_semaphore_signal(mySemaphore);
NSLog(@"Resources available, shouldn't wait");
}
哪个最终结果不会减少信号量但似乎是一个黑客,建议?
编辑
当我输入我在做什么以及我是如何得出这个结论时,我意识到我确实在以错误的方式思考解决方案,我只是想知道我是否在等待资源并能够显示'等待'对话框给用户。我认为这是正确的方法:
[self showWait];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_wait(mySemaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
[self hideWait];
});
});