gpt4 book ai didi

iphone - NSTimer 并在主线程上执行选择器

转载 作者:行者123 更新时间:2023-12-03 19:42:15 29 4
gpt4 key购买 nike

是否可以在主线程上运行由 nstimer 调用的选择器?

NSTimer 在它自己的线程中生成。

我的结构是一个线程调用一个带有 nstimer 的方法,nstimer 调用一个执行一些更新的方法,但我需要这些更新发生在主线程上。解决办法是什么?添加另一个方法并说 performOnMainThread?

最佳答案

是的,从您的计时器例程中只需调用“performOnMainThread”即可。来自文档:

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

还有一个关于使重复计时器无效的警告:

for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed

关于iphone - NSTimer 并在主线程上执行选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4954537/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com