是DispatchQueue.global(qos:.userInteractive).async与DispatchQueue.main.async相同 [英] Is DispatchQueue.global(qos: .userInteractive).async same as DispatchQueue.main.async

查看:1714
本文介绍了是DispatchQueue.global(qos:.userInteractive).async与DispatchQueue.main.async相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读教程:
https://www.raywenderlich.com/148513/grand-central-dispatch-tutorial-swift-3-part-1

遇到了QoS类用户交互式的定义。
它在那里提到它应该在主线程上运行。
那么,我的问题是

And came across the definition of QoS class User-interactive. Its mentioned there that this should run on main thread. So, my question is then what is the difference between the

DispatchQueue.global(qos: .userInteractive).async{} 

DispatchQueue.main.async{}

谢谢!!

推荐答案

此处描述了服务质量定义:

The "quality of service" definitions are described here:

https://developer.apple.com/library/content/ documentation / Performance / Conceptual / EnergyGuide-iOS / PrioritizeWorkWithQoS.html

看起来主线程的QoS等级为用户交互 。但是,仅仅因为创建的QoS具有用户交互的QoS,并不意味着它是主线程。

It looks like the "main" thread will have a QoS class of "User-interactive". However, just because a thread is created with a QoS of "User-interactive", does not mean that it is the "main" thread.

您可以在Xcode调试器。在异步块中放置一个断点,然后查看Debug Navigator活动线程面板。从主线程调用 DispatchQueue.global(qos:.userInteractive).async {} 时,它显示的名称与主线程不同。

You can observe this in the Xcode debugger. Put a breakpoint inside the async block and look at the Debug Navigator active thread panel. When DispatchQueue.global(qos: .userInteractive).async{} is called from the main thread, it displays with a different name than the main thread.

通常,主线程被视为应执行所有与视图相关的访问的特殊线程。如果某些东西会消耗很长时间,例如调用Web服务,压缩文件等,您将需要在单独的队列中运行代码,并且当该过程完成时,返回到更新用户界面的主队列。

In general, the main thread is considered the special thread where all view-related access should be performed. If something will consume any significant time, e.g. calling a web service, compressing a file, etc., you will want to run code in a separate queue, and when the process completes, return to the main queue where you update the user interface.

另请注意,在iOS 11中使用Xcode 9时,从非主线程访问用户界面对象时会发出警告。

Note also that when using Xcode 9 with iOS 11, a warning will be emitted when a user-interface object is accessed from a non-main thread.

这篇关于是DispatchQueue.global(qos:.userInteractive).async与DispatchQueue.main.async相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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