NSTimer是线程安全吗?

时间:2011-12-17 11:59:37

标签: objective-c ios cocoa-touch nstimer

我有一个间隔为1/4秒的重复计时器。我正在这样初始化它:

[NSTimer scheduledTimerWithTimeInterval:0.25 
                                 target:self 
                               selector:@selector(toggleCams) 
                               userInfo:nil 
                                repeats:YES];

这是否同步发生?换句话说,我是否保证在调用toggleCams的同时不会调用同一类中的另一个方法?

1 个答案:

答案 0 :(得分:18)

NSTimers实际上只是定期将事件激发到封闭的NSRunLoop中,每个线程都有(或应该有)。因此,如果你有一个在不同线程中运行的子(或后台)进程,NSTimers将触发该线程的NSRunLoop而不是应用程序的主NSRunLoop。

相关问题