DispatchQueue.main.async和DispatchQueue.main.sync之间的区别 [英] Difference between DispatchQueue.main.async and DispatchQueue.main.sync

查看:6181
本文介绍了DispatchQueue.main.async和DispatchQueue.main.sync之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用DispatchQueue.main.async很长一段时间来执行一些与UI相关的操作。
但Swift提供了DispatchQueue.main.async和DispatchQueue.main.sync,两者都在主队列中执行。
那么有谁可以告诉我他们之间的区别?
什么时候应该使用它们?
提前谢谢。

I have been using DispatchQueue.main.async since a long time to perform some UI related operations.

 But Swift provides DispatchQueue.main.async and DispatchQueue.main.sync and both are performed in main queue.

 So can anyone tell me difference between them? 

 And when should i use them?

 Thank you in advance.

        DispatchQueue.main.async {
            self.imageView.image = imageView
            self.lbltitle.text = ""

        }
        DispatchQueue.main.sync {
            self.imageView.image = imageView
            self.lbltitle.text = ""

        }


推荐答案

为什么要并发?
一旦你添加到你的应用程序,像数据加载这样的繁重任务会减慢你的UI工作速度甚至冻结它。
并发允许您同时执行2个或更多任务。
这种方法的缺点是线程安全性并不总是那么容易控制。 F.E.当不同的任务想要访问相同的资源时,例如尝试在不同的线程上更改相同的变量或访问已被不同线程阻止的资源。

Why concurrency? As soon as you add to your app heavy tasks like data loading it slows your UI work down or even freezes it. Concurrency lets you perform 2 or more tasks "simultaneously". The disadvantage of this approach is a thread safety which is not always as easy to control. F.e. when the different tasks want to access the same resources like trying to change the same variable on a different threads or accessing the resources already blocked by the different threads.

There`sa我们需要注意的几个抽象。

There`s a few abstractions we need to be aware with.


  • 队列。

  • 同步/异步任务性能。

  • 优先事项。

  • 常见问题。


  • Queues.
  • Synchronous/Asynchronous task performance.
  • Priorities.
  • Common troubles.

队列即可。

Queues.

必须序列并发。同时还有全局私有

在串行队列上,任务将由并发队列任务中的一个将同时执行,并将以意外的时间表完成。与并发队列相比,同一组任务将占用串行队列更长的时间。

On the serial queue tasks will be finished one by one while on the concurrent queue tasks will be performed simultaneously and will be finished with unexpected schedule. Same group of tasks will take the way more longer time on the serial queue comparing to the concurrent.

您可以创建自己的私人队列 serial 并发)或使用已有的全局(系统)队列
只有主队列全局队列中的串行

You can create your own private queues (both serial or concurrent) or use already available global (system) queues. Only main queue is the serial one among the global queues.

强烈建议不要执行主队列上的UI工作所未提及的繁重任务(从网络加载数据),而是在其他队列上执行,以保持UI解冻和响应用户操作。如果我们在其他队列上更改UI,则可以使用不同的意外计划和速度进行更改,以便可以在需要之前或在为时已晚时绘制一些UI元素。它可能会崩溃UI。还需要记住,由于全局队列系统队列,系统可以在其上运行一些其他任务。




服务质量/优先级

It is highly recommended not to perform heavy tasks which is not referred with the UI work on the main queue (f.e. loading data from the network), but on the other queues to keep the UI unfrozen and responsive to the user actions. If we let the UI be changed on the other queues the changes can be made with a different unexpected schedule and speed so some UI elements can be drawn before it's needed or when it's too late. It can crash the UI. Also need to keep in mind that since the global queues are system queues there are some other tasks can run by the system on them.


Quality of service / priority.

队列也有不同的 qos(服务质量),它设置执行优先级的任务(此处从最高到最低):

.userInteractive - 主队列
.userInitiated - 用户启动的任务,用户等待某些响应 .utility - 用于需要一些时间并且不需要立即响应的任务,例如使用数据 .background - 用于与视觉部分无关的任务对完成时间不严格。)
还有一个 .default 队列,它不会传输 qos 信息。
如果无法检测 qos .userInitiated .utility <之间将使用 qos / em>的。


Queues also have different qos (Quality of Service) which sets the task performing priority (from highest to lowest here):
.userInteractive - main queue
.userInitiated - for the user initiated tasks on which user waits for some response
.utility - for the tasks which takes some time and doesn't require immediate response, e.g working with data
.background - for the tasks which aren`t related with the visual part and which aren't strict for the completion time).

There is also

.default queue which does't transfer the qos information. If it wasn't possible to detect the qos the qos will be used between .userInitiated and .utility.

任务可以同步异步执行。


  • 同步函数仅在任务完成后才返回当前队列的控件。它会阻塞队列并等待任务完成。

  • Synchronous function returns the control on the current queue only after task is finished. It blocks the queue and waits until the task is finished.

异步函数在任务完成后立即返回当前队列的控制权发送到不同的队列。它不会等到任务完成。它不会阻止队列。

Asynchronous function returns control on the current queue right after task has been sent to be performed on the different queue. It doesn't wait until the task is finished. It doesn't block the queue.

常见问题。

程序员在投影并发应用程序时所犯的最常见错误如下:

The most popular mistakes programmers make while projecting the concurrent apps are following:


  • 竞争条件 - 当应用程序工作取决于代码部件执行的顺序时引起。

  • 优先级反转 - 当较小优先级任务的优先级较高的任务等待由于某些资源被阻止而完成时

  • 死锁 - 当几个队列无限等待时对于已经被其中一些队列阻止的来源(变量,数据等)。

  • Race condition - caused when the app work depends on the order of the code parts execution.
  • Priority inversion - when the higher priority task wait of the smaller priority task is finished due to some resources are blocked
  • Deadlock - when the few queues are infinitely wait for the sources (variables, data etc.) already blocked by some of these queues.

永远不要调用同步功能主队列

如果在主队列上调用sync函数,它将阻塞队列,队列将等待任务完成但任务永远不会完成,因为它甚至不能o由于队列已经被阻止而启动。它被称为死锁

何时使用同步?
当我们需要等到任务完成了。 F.E.当我们确保某些函数/方法不是双重调用时。 F.E.我们有同步并试图阻止它被双重调用,直到它完全结束。这里有一些关于这个问题的代码:
如何找出导致IOS设备错误崩溃报告的原因?

这篇关于DispatchQueue.main.async和DispatchQueue.main.sync之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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