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

前端 未结 6 700
梦如初夏
梦如初夏 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:03

    Found from the following documentation. https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_sync

    Unlike dispatch_async, "dispatch_sync" function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.

    Unlike with dispatch_async, no retain is performed on the target queue. Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, no Block_copy is performed on the block.

    As an optimization, this function invokes the block on the current thread when possible.

提交回复
热议问题