执行下一步运行的循环:这有什么错GCD? [英] Perform on Next Run Loop: What's Wrong With GCD?

查看:174
本文介绍了执行下一步运行的循环:这有什么错GCD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这两种方法:

dispatch_async(dispatch_get_main_queue(),^{
    [self handleClickAsync];
});

[self performSelector:@selector(handleClickAsync) withObject:nil afterDelay:0];

在响应按钮preSS。

in response to a button press.

第二个允许的UIButton 突出正如人们所预料,并在下次运行循环执行 handleClickAsync (我想:晚些时候肯定)。第一不允许的UIButton 实例点亮,直到操作是完全做得到。

The second allows the UIButton to highlight as one would expect and perform the handleClickAsync on the next run loop (I suppose: "sometime later" for sure). The first does not allow the UIButton instance to light up until the operation is completely done.

什么是GCD要做到这一点正确的方法,或者是 performSelector 仍是唯一的方法是什么?

What is the correct way to do this with GCD, or is performSelector still the only way?

推荐答案

我相信答案就在这里找到了<一个href=\"https://developer.apple.com/library/mac/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW2\">discussion主要的调度队列的:

I believe the answer is found here in a discussion of the main dispatch queue:

此队列与应用程序的运行循环的工作原理(如果有的话present)用的连接到运行循环其他事件源的执行交错​​排队任务的执行。

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.

在换言之,主调度队列设置一个第二队列(沿着由提供的标准事件队列UIApplicationMain()用于处理提交给主队列块。
当块都在排队present,运行循环将轮流从主事件队列和调度队列中出列任务。另一方面,在<一href=\"https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/doc/uid/20000050-performSelector_withObject_afterDelay_\">reference为延迟的参数 -performSelector:withObject:afterDelay:指出:

In other words, the main dispatch queue sets up a secondary queue (alongside the standard event queue provided by UIApplicationMain() for handling blocks submitted to the main queue. When blocks are present in the queue, the run loop will alternate dequeuing tasks from the main event queue and the dispatch queue. On the other hand, the reference for the delay parameter of -performSelector:withObject:afterDelay: notes that:

指定为0的延迟并不一定导致要立即执行的选择器。选择器仍然一旦排队的线程的运行循环,并进行越好。

Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.

因此​​,当你使用进行选择,操作在主事件队列的末尾排队,并且将直到队列中的一切都在它的前面(presumably包括code后进行到unhighlight的的UIButton )已被处理。当您使用主调度队列,但是,它增加了成块,然后可能会被立即处理二级队列(即,在接下来的运行循环)假设有在主队列中没有其他块。在这种情况下,code到unhighlight按钮仍然坐在主事件队列,而运行循环处理副块队列中的事件。

Thus, when you use perform selector, the operation is queued at the end of the main event queue, and will not be performed until after everything in front of it in the queue (presumably including the code to unhighlight the UIButton) has been processed. When you use the main dispatch queue, however, it adds the block into the secondary queue which then likely gets processed immediately (i.e., on the next run loop) assuming there are no other blocks in the main queue. In this case, the code to unhighlight the button is still sitting in the main event queue while the run loop processes the event from the secondary block queue.

这篇关于执行下一步运行的循环:这有什么错GCD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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