What will happen if I have nested dispatch_async calls?

淺唱寂寞╮ 提交于 2019-12-02 18:06:41

The dispatch_sync statement waits until the block it covers is executed completely. dispatch_async returns immediately and proceeds to the next line of code, so everything inside is happening in parallel.

If queue was a serial queue created by yourself, then:

Situation 1 - The root block returns immediately. Inside it waits for [self go....], and then goes to dispatch_async, which returns immediately as well.

Situation 2 - If queue was a serial queue, then there would be a dead lock since it will wait for itself to finish executing. Since you are dealing with asynchronous one, that block will be executed in parallel. (Thanks, @Ken Thomases)

Situation 3 - No need in dispatch_sync here. It causes the deadlock.

Situation 4 - Waits for [self ...], then returns immediately.

If you replace the queue with main queue, then remember to not dispatch_sync on main queue, because it will cause a deadlock (it will not if dispatched not from main thread, thanks @Ken Thomases).

To understand it better, replace your function with:

-(void)goDoSomethingLongAndInvolved:(NSString *)message {
    for(int i = 0; i < 50; ++i) {
        NSLog(@"%@ -> %d", message, i); 
    }
}

You will clearly see what's going on every time, whether it waits or not. Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!