Re: Observing Time
Re: Observing Time
- Subject: Re: Observing Time
- From: Rick Mann <email@hidden>
- Date: Thu, 25 Jul 2013 19:08:12 -0700
On Jul 25, 2013, at 18:52 , email@hidden wrote:
> I'm getting about a half second lag behind the system clock.
> This is actually reasonable for my purposes, but I wonder if I'm missing something that might be more in sync.
You can do this using NSTimers, but you need to make sure you adjust for the current fraction of a second. That is, if you simply set up a scheduled timer with a one-second interval, you'll get called every second, but it won't necessarily be at the instant the wall clock second ticks over.
So, thing to do is to create a repeating timer with a fire date rounded to the next second and a one-second interval. Something like this (written off-the-cuff, so might need some tweaking):
CFAbsoluteTime fireDateAbs = CFAbsoluteTimeGetCurrent();
fireDateAbs = trunc(fireDateAbs + 0.5); // Round to the next second
NSDate* fireDate = [NSDate dateWithTimeIntervalSinceReferenceDate: fireDateAbs];
mTimer = [[NSTimer alloc] initWithFireDate: fireDate interval: 1.0 target: self selector: @selector(update:) userInfo: nil repeats: true];
[[NSRunLoop currentRunLoop] addTimer: mTimer forMode: NSRunLoopCommonModes];
--
Rick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden