我们可以从后台线程更新UI吗? [英] Can we update UI from background Thread?

查看:93
本文介绍了我们可以从后台线程更新UI吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,iOS专家只是为了澄清我的概念,我对从主线程进行UI更新有些困惑.苹果的要求是所有与UI相关的东西都应该在主线程中执行.因此要进行测试:

Hello iOS experts just to clear my concept I have a bit confusion about UI updation from Main Thread. Apple requirements are that all UI related stuff should be carried out in main Thread.So to test:

情况1::我异步地将任务分配给全局分配并发队列.经过一些处理后,我直接从并发队列(后台线程)更新了我的UI内容,使用下面的代码可以正常工作.

Case1: I dispatch a task to a global dispatch concurrent queue asynchronously . After some processing I update my UI stuff directly from the concurrent queue (background thread), working fine using the below code.

dispatch_queue_t myGlobalQueue;
myGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(myGlobalQueue, ^{

     // Some processing
    // Update UI;
});

情况2:比起我尝试使用Apple要求的方法,异步地将一个块分配到全局分配并发队列.经过一些处理后,我使用以下代码更新了主线程中的UI内容:

Case2: Than i tried the Apple required way, dispatch a block to global dispatch concurrent queue asynchronously. After some processing I update the UI stuff in Main thread using below code:

dispatch_queue_t myGlobalQueue;
myGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(myGlobalQueue, ^{

    // Some processing

    dispatch_async(dispatch_get_main_queue(), ^{
        // UI Updation
    });
});

现在,在两种情况下,我都得到相同的结果.现在听我的问题

Now in both cases I am getting the same result. Now listen my questions

问题: 1:如果情况1仍在主线程中而不在后台线程中?如果是这样,为什么苹果医生说:

Questions: 1: In case1 are we still in Main Thread not in background thread ? If so than why Apple doc say:

并发队列(也称为全局调度队列的一种)可以并发执行一个或多个任务,但是任务仍在 将它们添加到队列的顺序. 当前 执行任务在不同线程上运行,这些线程由 调度队列.在任何给定的位置执行的确切任务数 点是可变的,取决于系统条件.

Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue. The exact number of tasks executing at any given point is variable and depends on system conditions.

现在,如果我们位于主线程上,则这与Apple Doc的粗体部分矛盾.

Now if we are on Main Thread than this is a contradiction with the bold part of Apple Doc.

2 :在Case1中,如果我们处于后台线程中,那为什么Apple要求获取主线程来进行UI更新,即使我们也可以从后台线程中更新UI?

2: In Case1 if we are in background thread, than why Apple require to get Main Thread for UI Updation, Even though we can update UI from background Thread too?.

请仔细阅读我的问题,并建议我是否做错了什么.您的帮助和时间将不胜感激.

Kindly read my question fully and suggest me if I am doing something wrong. Your help and time would be greatly appreciated.

推荐答案

至1)

这只是说,来自同一队列的任务 可以在不同的线程上运行.这并不是说任务不能在特定线程上运行. (但我真的不希望在主线程上运行任务.)

This simply says, that tasks from the same queue can run on distinct threads. It does not say, that a task cannot run on a specific thread. (But I really do not expect to run a task on the main thread.)

至2)

Apple并不是说,在每种情况下从其他线程 更新UI都会失败,但是可以失败.您不应该这样做:一次它将失败.

Apple does not say, that updating the UI from a different thread will fail in every case, but can fail. You shouldn't do it: One time it will fail.

您应该阅读以下内容:

https://en.wikipedia.org/wiki/Necessity_and_sufficiency

这篇关于我们可以从后台线程更新UI吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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