How do I create an accurate timer event in Objective-C/iOS?

ios, nstimer, objective-c, timer

Solution

Try CADisplayLink. It fires at the refresh rate (60 fps).

CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(timerFired:)];
displayLink.frameInterval = 2;
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

This will fire every 2 frames, which is 30 times per seconds, which seems to be what you are after.

Note, that this is tied to video frame processing, so you need to do your work in the callback very quickly.

Problem

I'm looking to create a countdown timer for SMPTE Timecode (HH:MM:SS:FF) on iOS. Basically, it's just a countdown timer with a resolution of 33.33333ms. I'm not so sure NSTimer is accurate enough to be counted on to fire events to create this timer. I would like to fire an event or call a piece of code every time this timer increments/decrements. I'm new to Objective-C so I'm looking for wisdom from the community. Someone has suggested the CADisplayLink class, looking for some expert advice.

Original source