Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer is it thread safe?

I have a repeated timer with interval of 1/4 second. I am initializing it like this:

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

Does this happen synchronously? In other words, am I guaranteed that another method in the same class will not be called at the same time toggleCams is called?

like image 243
0xSina Avatar asked Dec 17 '11 11:12

0xSina


1 Answers

The NSTimers actually just periodically fire events into the enclosing NSRunLoop, which each thread has (or should have). So, if you have a child (or background) process running in a different thread, the NSTimers will fire against that thread's NSRunLoop instead of the application's main NSRunLoop.

like image 133
Saurabh Avatar answered Sep 28 '22 13:09

Saurabh