ios – 调度同步的目的是什么?

我很清楚dispatch_async队列正在执行什么,但我不清楚dispatch_sync的用途是什么.举个例子:这有什么区别:

NSLog(@"A");
NSLog(@"B");

还有这个:

dispatch_sync(dispatch_get_main_queue(), ^ {
NSLog(@"A");
    });
NSLog(@"B");

正如我所知,在两种方式中输出将是A然后是B.因为同步是按照写入的顺序执行的.谢谢.

最佳答案 根据
Docs

Submits a block to a dispatch queue for synchronous execution. Unlike
dispatch_async, this 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.

点赞