Swift线程处理:什么时候使用DispatchQueue.main.async? [英] Swift Threading: When to use DispatchQueue.main.async?

查看:1769
本文介绍了Swift线程处理:什么时候使用DispatchQueue.main.async?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我知道调用它时调度队列在做什么,但是我不确定何时确切应该使用它,以及当我使用它时它的优点是什么.

I believe I understand what the dispatch queue is doing when I call it, but I'm not sure when exactly I should use it and what it's advantages are when I do use it.

如果我的理解是正确的,则DispatchQueue.main.async { // code }将安排闭包中包含的代码以异步方式在主调度队列上运行.主队列具有最高优先级,通常保留用于更新UI以最大程度地提高App响应速度.

If my understanding is correct, DispatchQueue.main.async { // code } will schedule the code contained within the closure to run on the main dispatch queue in an asynchronous manner. The main queue has the highest priority, and is typically reserved for updating UI to maximize App responsiveness.

让我感到困惑的是:更新调度队列闭包中的UI元素与只在闭包外的同一位置编写代码有什么不同?在加载了方法的视图主体中执行代码而不是将其发送到调度队列中,执行代码的速度更快吗?如果没有,为什么?

Where I'm confused is: What exactly is the difference in updating UI elements within a dispatch queue closure versus just writing the code outside the closure in the same spot? Is it faster to execute the code in the body of a view did load method rather than sending it to the dispatch queue? If not, why?

代码示例:

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
    }

}

对:

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        DispatchQueue.main.async {
            updateUI()
        }
    }
}

哪个会更快更新UI?

推荐答案

DispatchQueue.main.async的主要用途是当您在后台队列上运行代码并且需要在主队列上执行特定的代码块时.

The primary use of DispatchQueue.main.async is when you have code running on a background queue and you need a specific block of code to be executed on the main queue.

在您的代码中,viewDidLoad已经在主队列上运行,因此没有理由使用DispatchQueue.main.async.

In your code, viewDidLoad is already running on the main queue so there is little reason to use DispatchQueue.main.async.

但是使用它不一定是错误的.但这确实改变了执行顺序.

But isn't necessarily wrong to use it. But it does change the order of execution.

不包含以下示例:

class MyViewController: UIViewController {
    func updateUI() {
        print("update")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("before")
        updateUI()
        print("after")
    }
}

正如人们所期望的那样,输出将是:

As one might expect, the output will be:

之前
更新
之后

before
update
after

现在添加DispatchQueue.main.async:

class MyViewController: UIViewController {
    func updateUI() {
        print("update")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("before")
        DispatchQueue.main.async {
            updateUI()
        }
        print("after")
    }
}

输出更改:

之前
之后
更新

before
after
update

这是因为异步闭包在当前runloop完成之后排队等待运行.

This is because the async closure is queued up to run after the current runloop completes.

这篇关于Swift线程处理:什么时候使用DispatchQueue.main.async?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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