简体   繁体   English

我们需要在主线程或主队列中更新UI吗?

[英]Do we need to update UI in main thread or in main queue?

I read a lot many articles which specifies that we need to update the UI in the main thread however whenever I update my UI is always the code. 我读了很多文章,指出我们需要更新主线程中的UI,但是每当我更新我的UI时总是代码。

DispatchQueue.main which in return gives me the Queue not thread. DispatchQueue.main反过来给了我Queue而不是线程。 How exactly I would access the thread or both are same ? 我将如何访问该线程或两者是一样的?

Imagine a train station where the number of the train is the same as the number of the platform that it leaves from. 想象一下火车站,火车的数量与它离开的平台的数量相同。

So if you want the #1 train, you stand on #1 platform. 因此,如果您想要#1列车,那么您就站在#1平台上。 You cannot get on the train without first standing on the platform. 没有先站在平台上就无法上火车。 Everyone else who wants to get on this train also stands on the platform to wait for their chance to get on the train. 其他想乘坐这列火车的人也站在平台上等待他们上火车的机会。

The train is the thread. 火车是线程。 The platform is the queue. 平台就是队列。

If you want to get on the main thread, get on the main queue. 如果你想进入主线程,请进入主队列。

DispatchQueue manages the execution of the code on a specific thread. DispatchQueue管理特定线程上代码的执行。

From Apple documentation: 来自Apple文档:

DispatchQueue manages the execution of work items. DispatchQueue管理工作项的执行。 Each work item submitted to a queue is processed on a pool of threads managed by the system. 提交到队列的每个工作项都在系统管理的线程池上处理。

So, when you call 所以,当你打电话的时候

DispatchQueue.main.async {
        //your code
}

This code is submitted to the main queue which in turn runs on the Main thread. 此代码将提交到main队列,而main队列又在主线程上运行。

From Dispatch Queues in the Concurrency Programming Guide: 从并发编程指南中的Dispatch Queues

Main dispatch queue 主调度队列

The main dispatch queue is a globally available serial queue that executes tasks on the application's main thread. 主调度队列是一个全局可用的串行队列,它在应用程序的主线程上执行任务。 This queue works with the application's run loop (if one is present) to interleave the execution of queued tasks with the execution of other event sources attached to the run loop. 此队列与应用程序的运行循环(如果存在)一起工作,以将排队任务的执行与附加到运行循环的其他事件源的执行交错​​。 Because it runs on your application's main thread, the main queue is often used as a key synchronization point for an application. 因为它在应用程序的主线程上运行,所以主队列通常用作应用程序的关键同步点。

Generally, GCD maintains a pool of threads, and there is no 1-1 relationship between dispatch queues and threads. 通常,GCD维护一个线程池,并且调度队列和线程之间没有1-1关系。 But the main queue is special: it is tied to the main thread, all items dispatched to the main queue are executed on the main thread. 但主队列是特殊的:它绑定到主线程,调度到主队列的所有项都在主线程上执行。 (The same is true for OperationQueue.main .) (对于OperationQueue.main也是如此。)

Dispatching code to DispatchQueue.main (or OperationQueue.main ) ensures that it is executed on the main thread, and synchronized with other UI updates. 将代码调度到DispatchQueue.main (或OperationQueue.main )可确保它在主线程上执行,并与其他UI更新同步。

In this sense, the terms “execute on the main thread” and “execute on the main queue” are often used interchangeably. 在这个意义上,术语“在主线程上执行”和“在主队列上执行”通常可互换使用。

//main thread //主线程

DispatchQueue.main.async
{ 
 //eg. 
 tableview.reloadData()
  // here you update your UI.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM