使用[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]]让计划的选择器触发 [英] Using [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] to let the scheduled selectors fire

查看:467
本文介绍了使用[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]]让计划的选择器触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一些消息计划使用 performSelector



在处理UI操作时,我需要等待所有当前排定的选择器触发。



我可以将我的代码放在另一个方法中,并使用 performSelector:target:argument:order:modes:使用 order 值足够高以确保它最后激活,但有原因为什么我认为这将是一个丑陋的解决方案。



因此我发送 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] ,它似乎工作,就像我需要它。



仍然,我不知道这是一个合法的方式让运行循环滚动只需一次迭代。

解决方案

好的,回答我自己的问题。



首先是重复(另外,)。



接下来,通常,在同一个运行循环中发送 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]]


  1. 理论上,autorelease pool会以这种方式消耗。在实践中,我不能通过使用分配了前 - runUntilDate (在ARC下)的对象使我的应用程序崩溃,但最好不要冒险。

    li>

TL; DR我应该为自己一个忙,用更确定的东西替换那段代码。


In my application at some point I have a bunch of messages scheduled using performSelector.

Under some conditions, while handling an UI action, I need to wait for all the currently scheduled selectors to fire.

I could place my code in another method and schedule it using performSelector:target:argument:order:modes: with order value high enough to be sure it will fire last, but there are reasons why I think that would make an ugly solution.

So I send [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] and it seems to work just like I need it to.

Still, I'm not sure if that is a legitimate way to let the run loop roll for just one iteration. Also, are there any potential troubles associated with this approach?

解决方案

Okay, answering my own question.

First of all it's a duplicate (also, this).

Next, generally, sending [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] from within the same run loop is a bad idea:

  1. In theory, autorelease pool will get drained that way. In practice, I've not been able to make my app crash by using objects allocated pre-runUntilDate (under ARC), but better not to risk anyway.

  2. If somehow another action gets dequeued during that runUntilDate it might cause some unexpected side effects.

TL;DR I should do myself a favor and replace that piece of code with something more deterministic.

这篇关于使用[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]]让计划的选择器触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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