NSTimer是线程安全吗?

0xS*_*ina 6 cocoa-touch objective-c nstimer ios

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

[NSTimer scheduledTimerWithTimeInterval:0.25 
                                 target:self 
                               selector:@selector(toggleCams) 
                               userInfo:nil 
                                repeats:YES];
Run Code Online (Sandbox Code Playgroud)

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

Sau*_*abh 19

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