Swift:更新UI-主线程上的整个函数还是UI更新? [英] Swift: Update UI - Entire function on main thread or just the UI update?

查看:57
本文介绍了Swift:更新UI-主线程上的整个函数还是UI更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读到UI应该总是在主线程上更新.但是,对于实现这些更新的首选方法,我有些困惑.

I've read that the UI should always be updated on the main thread. However, I'm a little confused when it comes to the preferred method to implement these updates.

我具有执行某些条件检查的各种功能,然后将结果用于确定如何更新UI.我的问题是整个功能应该在主线程上运行吗?UI应该更新吗?我可以/应该在另一个线程上运行条件检查吗?是否取决于功能的作用或要完成的速度有多快?

I have various functions that perform some conditional checks then the result is used to determine how to update the UI. My question is should the entire function run on the main thread? Should just the UI update? Can / should I run the conditional checks on another thread? Does it depend on what the function does or how fast you want it done?

示例函数,该函数无需穿线即可更改ImageView中的图像:

@IBAction func undoPressed(_ sender: Any) {
        if !previousDrawings.isEmpty {
            previousDrawings.remove(at: previousDrawings.count - 1)
            if let lastDrawing = previousDrawings.last {
                topImageView.image = lastDrawing
            }
            else {
                // empty
                topImageView.image = nil
            }
        }
    }

我应该在主线程上设置 topImageView.image 吗?像这样:

Should I be setting topImageView.image on the main thread? Like this:

 @IBAction func undoPressed(_ sender: Any) {
        if !previousDrawings.isEmpty {
            previousDrawings.remove(at: previousDrawings.count - 1)
            if let lastDrawing = previousDrawings.last {
                DispatchQueue.main.async {
                    self.topImageView.image = lastDrawing
                }
            }
            else {
                DispatchQueue.main.async {
                    self.topImageView.image = nil
                }
            }
        }
    }

我应该使用后台线程进行条件检查吗?像这样:

@IBAction func undoPressed(_ sender: Any) {
        DispatchQueue.global(qos: .utility).async {
            if !previousDrawings.isEmpty {
                previousDrawings.remove(at: previousDrawings.count - 1)
                if let lastDrawing = previousDrawings.last {
                    DispatchQueue.main.async {
                        self.topImageView.image = lastDrawing
                    }
                }
                else {
                    DispatchQueue.main.async {
                        self.topImageView.image = nil
                    }
                }
            }
        }
    }

如果有人可以解释首选哪种方法,以及为什么这样做真的有帮助.

If someone could explain what method is preferred and why that would be really helpful.

推荐答案

首先,您的 undoPressed 方法将在主队列上调用.

First off, your undoPressed method will be called on the main queue.

在第一组代码中,所有内容都将放在主队列中.

In the first set of code, everything will be on the main queue.

在第二组代码中,使用 DispatchQueue.main.async 是没有意义的,因为其余代码已在主队列中.

In the second set of code, using DispatchQueue.main.async is pointless since the rest of the code is already on the main queue.

所以实际上,您仅有的两个明智的选择是1和3.

So really your only two sensible options are 1 and 3.

给出您的代码,选项1很好.如果在后台运行的代码花费的时间不多,那么您只想使用选项3.由于您这里的代码很简单,几乎不需要时间来执行,因此这里的选项3没有意义.

Given your code, option 1 is fine. You would only want to use option 3 if the code being run in the background took more than a trivial amount of time to execute. Since the code you have here is trivial and will take virtually no time to execute, there is no point in option 3 here.

因此,只需使用第一组代码,就可以了.

So simply use your first set of code and you'll be fine.

担心在需要执行大循环或计算复杂算法或执行任何类型的网络访问时将代码移至后台.

Worry about moving code to the background when it need to perform a big loop or calculate a complicated algorithm or perform any sort of network access.

这篇关于Swift:更新UI-主线程上的整个函数还是UI更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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