OGeek|极客世界-中国程序员成长平台

标题: ios - dispatch_async 何时实际调度? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:28
标题: ios - dispatch_async 何时实际调度?

假设我有以下代码:

dispatch_async(dispatch_get_main_queue()) {
    myFunction()
}

这表示异步调用 block ,它调用 myFunction。假设我在我的主队列中调用了这段代码,这也是为 dispatch_async 调用指定的队列。

在这种情况下,这个 block 什么时候真正被调用?我当前的队列是否被抢占并且 block 立即运行,或者当前调用堆栈展开并且 block 在下一个事件循环中被调用?或者是其他东西?



Best Answer-推荐答案


When does this block actually get called in this case? Does my current queue get pre-empted and the block run immediately, or does the current call stack unroll and the block gets called at the next event loop? Or something else?

简而言之,如果您从主队列异步分派(dispatch)到主队列,则分派(dispatch)的 block 将不会运行,直到您返回到主运行循环(并且在分派(dispatch)到主队列的任何其他 block 也完成之后)。

关于ios - dispatch_async 何时实际调度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33204684/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4