Grand Central Dispatch(GCD)与performSelector - 需要更好的解释 [英] Grand Central Dispatch (GCD) vs. performSelector - need a better explanation

查看:217
本文介绍了Grand Central Dispatch(GCD)与performSelector - 需要更好的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了GCD和performSelectorOnMainThread:waitUntilDone,并且倾向于认为它们是可互换的 - 也就是说,performSelectorOnMainThread:waitUntilDone是GCD C语法的Obj-C包装器。我一直在考虑这两个命令是等价的:

I've used both GCD and performSelectorOnMainThread:waitUntilDone in my apps, and tend to think of them as interchangeable--that is, performSelectorOnMainThread:waitUntilDone is an Obj-C wrapper to the GCD C syntax. I've been thinking of these two commands as equivalent:

dispatch_sync(dispatch_get_main_queue(), ^{ [self doit:YES]; });


[self performSelectorOnMainThread:@selector(doit:) withObject:YES waitUntilDone:YES];

我不正确吗?也就是说,performSelector *命令与GCD命令有区别吗?我已经阅读了很多关于它们的文档,但还没有看到明确的答案。

Am I incorrect? That is, is there a difference of the performSelector* commands versus the GCD ones? I've read a lot of documentation on them, but have yet to see a definitive answer.

推荐答案

performSelectorOnMainThread: 使用GCD向主线程上的对象发送消息。

performSelectorOnMainThread: does not use GCD to send messages to objects on the main thread.

以下是< a href =http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/doc/uid/20000050-CJBEHAEF>文档表示该方法已实现:

Here's how the documentation says the method is implemented:

- (void) performSelectorOnMainThread:(SEL) selector withObject:(id) obj waitUntilDone:(BOOL) wait {
  [[NSRunLoop mainRunLoop] performSelector:selector target:self withObject:obj order:1 modes: NSRunLoopCommonModes];
}

并且在 performSelector:target:withObject:order:模式:,文档说明:


此方法设置一个计时器来对当前执行aSelector消息线程在下一个运行循环迭代开始时的运行循环。定时器配置为以modes参数指定的模式运行。当计时器触发时,线程会尝试从运行循环中取消消息并执行选择器。如果运行循环正在运行且处于指定模式之一,则成功;否则,计时器等待直到运行循环处于其中一种模式。

This method sets up a timer to perform the aSelector message on the current thread’s run loop at the start of the next run loop iteration. The timer is configured to run in the modes specified by the modes parameter. When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector. It succeeds if the run loop is running and in one of the specified modes; otherwise, the timer waits until the run loop is in one of those modes.

这篇关于Grand Central Dispatch(GCD)与performSelector - 需要更好的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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