global().async中的main.sync是什么意思? [英] What does main.sync in global().async mean?

查看:148
本文介绍了global().async中的main.sync是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中,有时会使用这种模式.

In Swift, I used this kind of pattern sometimes.

DispatchQueue.global().async {
    // do stuff in background, concurrent thread

    DispatchQueue.main.sync {
        // update UI
    }
}

此模式的目的很明确.在全局线程中进行耗时的计算,以使UI不会被锁定,并在计算完成后更新主线程中的UI.

The purpose of this pattern is clear. Do time consuming calculation in global thread so UI is not locked and update UI in main thread after calculation is done.

如果没有什么要计算怎么办?我刚刚在我的项目中发现了一个逻辑

What if there's nothing to calculate? I just found a logic in my project which

//A
DispatchQueue.main.sync {
    // do something
}

崩溃但

// B
DispatchQueue.global().async {
    DispatchQueue.main.sync {
        // do something
    }
}

不会崩溃.

它们有何不同?案例 B 与此不同吗?

How are they different? And Is case B different with just this?

// C
DispatchQueue.main.async {
    // do something
}

还有一个问题.我知道主线程是串行队列,但是如果我在多个main.async中运行多个代码块,它将像并发队列一样工作.

And one more question. I know main thread is serial queue, but if I run multiple code block in multiple main.async, it works like concurrent queue.

DispatchQueue.main.async {
    // do A
}

DispatchQueue.main.async {
    // do B
}

如果主线程确实是一个串行队列,那么它们如何同时运行?如果只是时间片,那么它们与全局并发队列有什么不同,除了主线程可以更新UI之外?

If main thread is really a serial queue, how can they run simultaneously? If it is just a time slicing than how are they different with global concurrent queue other than main thread can update UI?

推荐答案

x.sync表示调用队列将暂停并等待,直到同步块结束为止.所以在您的示例中:

x.sync means that the calling queue will pause and wait until the sync block finishes to continue. so in your example:

DispatchQueue.global().async {
    // yada yada something
    DispatchQueue.main.sync {
        // update UI
    }
    // this will happen only after 'update UI' has finished executing
}

通常,您不需要sync回到main,异步可能就足够好并且更安全,可以避免死锁.除非是特殊情况,否则您需要等到main完成后再继续执行异步任务.

Usually you don't need to sync back to main, async is probably good enough and safer to avoid deadlocks. Unless it is a special case where you need to wait until something finishes on main before continuing with your async task.

关于一个示例崩溃-调用同步并定位当前队列是一个死锁(调用队列等待同步块完成,但是由于目标队列(相同)正忙于等待对sync的调用而无法启动)完成),那可能就是崩溃的原因.

As for A example crashing - calling sync and targeting current queue is a deadlock (calling queue waits for the sync block to finish, but it does not start because target queue (same) is busy waiting for the sync call to finish) and thats probably why the crash.

关于使用异步方式在主队列上调度多个块:它们不会并行运行-它们将一个接一个地发生. 也不要假设队列==线程.将多个块调度到同一队列中,可能会创建系统允许的尽可能多的线程.只是主队列是利用主线程的特殊之处.

As for scheduling multiple blocks on main queue with async: they won't be run in parallel - they will happen one after another. Also don't assume that queue == thread. Scheduling multiple blocks onto the same queue, might create as many threads as system allow. Just the main queue is special that it utilises Main thread.

这篇关于global().async中的main.sync是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆