选项 1) 添加第二个串行队列是否有意义,以便 -method 中的代码仅在没有其他对该方法的调用正在执行时运行?
例如,在调用者中,您将拥有:
mySerialQueue = dispatch_queue_create("com.myapp.my-serial-queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(mySerialQueue, [self method]);
选项 2)使 myQueue 串行而不是并发(我假设它是并发的,因为 dispatch_barrier_async() 仅适用于您拥有的并发队列)。
myQueue = dispatch_queue_create("com.myapp.myqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(myQueue,^{ CODE 1});
dispatch_async(myQueue,^{ CODE 2});
dispatch_async(myQueue,^{
SOME CODE
dispatch_async(myQueue,^{
WAIT_CODE
dispatch_async(dispatch_get_main_queue(), ^{GUI UPDATE }
});
});
选项 3) 重新排序代码以在 SOME_CODE 之后添加 WAIT_CODE
dispatch_async(myQueue,^{ CODE 1});
dispatch_async(myQueue,^{ CODE 2});
dispatch_barrier_async(myQueue,^{
SOME CODE // this code must block
WAIT_CODE // this code must block
dispatch_async(dispatch_get_main_queue(), ^{ GUI UPDATE }
});