(iphone) start timer from background thread?

人盡茶涼 提交于 2019-12-12 05:39:21

问题


I create a timer with [NSTimer scheduledTimerWithTimerInterval:target:selector:...] from a background thread
It seems the timer created from a background thread doesn't call the selector given as the argument.

Is there something special I need to run a timer from background thread?
Thank you

  • EDIT

Here's the sequence

performSelectorInBackground: pushViewController

from the viewController's init sequence,

I alloc a timer with the above method.  

and the timer selector won't get called.


回答1:


You should only do things to the UI - like pushing view controllers and changing UI items - from the main thread. If you don't, things break, as you can see.

See the Cocoa Fundamentals Guide section titled "Are the Cocoa Frameworks Thread Safe?": it says "All UIKit objects should be used on the main thread only."

Original answer

There's nothing special needed. The selector will get called on the thread you did the scheduleTimer call from.



来源:https://stackoverflow.com/questions/5484510/iphone-start-timer-from-background-thread

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