I’m using an NSTimer to update a label that shows the current time each minute. I fire the timer at a full minute date (e.g. 2:23:00 instead of 2:22:17). The problem is that NSTimer seems to work inaccurately, which means it sometimes fires too early (which results in (e.g.) 2:23 PM when it is really 2:24 PM).
Is there any fix / workaround?
EDIT: Here’s how I start the timer:
NSTimer *clockTimer = [[NSTimer alloc] initWithFireDate:[NSDate nextFullMinuteDate]
interval:60.0
target:self
selector:@selector(updateClocks:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:clockTimer
forMode:NSDefaultRunLoopMode];
[clockTimer release];
Btw, I have tried adding a few milliseconds to the fire date, this seems to help but it looks like finding an appropriate interval to add is hit-and-miss.
EDIT 2: Ok, so I manually set my timer to fire 0.05 seconds after a full minute and then again after 60.0 seconds. Now here’s what I logged in the called method:
0.048728
0.047153
0.046484
0.044883
0.043103
As you can see, NSTimer doesn’t actually use a 60 second time interval, but a slightly shorter one.
Well, I just wrote my own little
NSTimerwrapper, calledMinuteTimerto “solve” the problem. I’ve uploaded it on pastebin. The idea is to reset the timer whenever it has fired too quickly so often that the date risks being displayed incorrectly.Since this is duct-tape programming, I’m still eager to hear other answers!