objective-c – “performSelector:withObject:afterDelay:”之间的区别是什么,其中delay是0,只是调用选择器?

前端之家收集整理的这篇文章主要介绍了objective-c – “performSelector:withObject:afterDelay:”之间的区别是什么,其中delay是0,只是调用选择器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个使用第三方SDK的iPad项目,并且已经阅读了SDK代码以了解它和 objective-c.我在异步回调中遇到了以下行:
[self performSelector:@selector(doSomething) withObject:nil afterDelay:0];

我在那个选择器上阅读了the documentation.相关的行似乎如下:

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.

我无法确定为什么要编写[self performSelector:@selector(doSomething)withObject:nil afterDelay:0],而不是仅仅编写[self doSomething].在我看来,延迟为零意味着应该立即进行呼叫.显然我遗漏了一些东西,框架作者不太可能随意选择这种方法.其他StackOverflow答案也没有说明这一点.是否使用了“performSelector”选择器,因为doSomething选择器本身是异步的,并且调用是在异步回调中进行的?

我确实发现another link建议如下:

It’s interesting to note that using performSelector with afterDelay will also let the warning [viz,a compiler warning] go away: …

[self performSelector:aSelector withObject:nil afterDelay:0.0];

那么作者是否使用此代码仅抑制编译器警告?如果是这种情况,那么最好通过clang push / pops来抑制警告,如this thread所示.

如果有人有一个有说服力的解释为什么作者使用… afterDelay方法,我将不胜感激.谢谢.

编辑2012年10月22日:我不相信@rmaddy给出的唯一答案完全适用于我的情况,但它仍然是一个很好的答案,所以我会接受它.我会密切关注这个问题,如果我发现任何新的东西,我会回来.谢谢. J.Z.

@H_502_26@

解决方法

一种可能的用途:
[self performSelector:@selector(doSomething) withObject:nil afterDelay:0];

是在调用’doSomething’之前允许当前的runloop完成.换句话说,在调用’doSomething’之前,可以完成当前调用堆栈(当前方法返回).通常,这用于为UI提供更新的机会.

虽然没有更多的背景,但很难知道作者的真实意图,但这是一种常见的用法.

@H_502_26@ @H_502_26@ 原文链接:https://www.f2er.com/c/119737.html

猜你在找的C&C++相关文章