且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在后台线程上创建NSTimer?

更新时间:2023-02-09 18:50:25

定时器将需要安装进入在已经运行的后台线程上运行的运行循环。那个线程将不得不继续运行运行循环以使定时器实际触发。而对于后台线程继续能够触发其他定时器事件,它将需要产生一个新的线程来实际处理事件(假设,当然,你正在做的处理需要大量的时间)。

The timer would need to be installed into a run loop operating on an already-running background thread. That thread would have to continue to run the run loop to have the timer actually fire. And for that background thread to continue being able to fire other timer events, it would need to spawn a new thread to actually handle events anyway (assuming, of course, that the processing you're doing takes a significant amount of time).

无论值多少,我认为通过使用Grand Central Dispatch或 NSBlockOperation 生成一个新线程来处理计时器事件完全合理地使用你的主线程。

For whatever it's worth, I think handling timer events by spawning a new thread using Grand Central Dispatch or NSBlockOperation is a perfectly reasonable use of your main thread.