Using [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] to let the scheduled selectors fire

允我心安 提交于 2020-01-25 01:58:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10371971/using-nsrunloop-currentrunloop-rununtildatensdate-date-to-let-the-schedul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!