Why can't we use a dispatch_sync on the current queue?

前端 未结 6 706
梦如初夏
梦如初夏 2020-11-28 20:51

I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn\'t know which until runtime (using St

6条回答
  •  余生分开走
    2020-11-28 21:11

    I found this in the documentation (last chapter):

    Do not call the dispatch_sync function from a task that is executing on the same queue that you pass to your function call. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously using the dispatch_async function.

    Also, I followed the link that you provided and in the description of dispatch_sync I read this:

    Calling this function and targeting the current queue results in deadlock.

    So I don't think it's a problem with GCD, I think the only sensible approach is the one you invented after discovering the problem.

提交回复
热议问题