In https://www.raywenderlich.com/3648-ios-concurrency-with-gcd-and-operations/lessons/2, Audrey Tam says that dispatching synchronously onto the current queue will cause a deadlock. Why is that? How does it do that? I’m not able to understand this.
When you dispatch to a queue, you are adding a block to the “todo list” of the queue. It will execute the blocks in its “todo list” in the order received.
If your code is the currently executing block, then your code has to finish before the queue moves on to the next block.
If you dispatch synchronously, your code has to wait until the block you dispatched is executed, before continuing. But the queue won’t start on the block you dispatched until it is finished with the currently executing block - which is you.
So your code is waiting for the next block to finish, and the queue is waiting for your code to finish before running the next block. Deadlock.
Thanks. That makes sense.
This topic was automatically closed after 166 days. New replies are no longer allowed.