Alex
Alex

Reputation: 3971

When Does NSTimer Fire If Main Thread Is Busy?

When does an NSTimer fire if the main thread is busy? Here is the line of code in question:

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

I'm a bit new to NSTimer (and run loops and threads) so thank you for the help. My app does a refresh on an NSTimer that was scheduled on the main thread. There may be issues if the refresh timer fires at the exact time interval specified and does not wait for the main thread to free up.

An example would be if the user does some action on the screen that is linked to data, and then the refresh fires immediately after and changes that data, causing the app to crash. Is this likely to happen, or will the refresh not fire until the users action has been resolved on the main thread?

Upvotes: 0

Views: 388

Answers (1)

Rob
Rob

Reputation: 437381

NSTimer schedules the timer on the current run loop (i.e. main run loop in this example) and so if the main thread is busy, the timer won't fire until you yield back to the main run loop.

See Timer Programming Topics and the NSTimer Class Reference, for more information.

Upvotes: 1

Related Questions