Re: Observing Time
Re: Observing Time
- Subject: Re: Observing Time
- From: email@hidden
- Date: Sun, 28 Jul 2013 11:43:02 +0900
Except for the reschedule after each fire, and the CFAbsoluteTimeGetCurrent call
This is pretty much what I tried.
I had done it with [[NSDate date] timeIntervalSince1970] which might be a hair longer than a CF call.
I will give it a whirl with this approach and compare.
At this point it's curiosity.
But I've learnt dispatch timer in the process which will come in handy in the future for some non GUI things.
Thanks to all
Sent from my iPhone
On 2013/07/28, at 11:28, Andy Lee <email@hidden> wrote:
> On Jul 27, 2013, at 9:57 PM, Andy Lee <email@hidden> wrote:
>> I'm trying again, this time creating a new NSTimer each time as suggested by Scott Ribe. Will let it run a while and see if I notice any drift.
>
> Looks pretty solid after several minutes -- as I would expect. To repeat Scott's suggestion:
>
>> Personally, I'd probably just do a non-repeating timer, set to fire at the next second roll-over. Alloc'ing, scheduling and releasing 1 timer per second is not a lot of overhead.
>
> I didn't take my own suggestion to add .001 and it worked fine, though for all I know it might have a problem once every 10,000 times the app is run.
>
> I stole Rick Mann's code and replaced trunc() with round().
>
> --Andy
>
> ==========
>
> @implementation AppDelegate
>
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
> {
> [self _resetTimer];
> }
>
> - (void)_resetTimer
> {
> CFAbsoluteTime fireDateAbs = round(CFAbsoluteTimeGetCurrent() + 0.5); // Round to the next second
> NSDate *fireDate = [NSDate dateWithTimeIntervalSinceReferenceDate:fireDateAbs];
>
> [[self timer] invalidate];
> [self setTimer:[[NSTimer alloc] initWithFireDate:fireDate
> interval:1.0
> target:self
> selector:@selector(_timerDidFire:)
> userInfo:nil
> repeats:NO]];
> [[NSRunLoop currentRunLoop] addTimer:[self timer] forMode:NSRunLoopCommonModes];
> }
>
> - (void)_timerDidFire:(NSTimer *)theTimer
> {
> [self _updateTimeDisplay];
> [self _resetTimer];
> }
>
> - (void)_updateTimeDisplay
> {
> NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
>
> [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
> [dateFormatter setDateStyle:NSDateFormatterNoStyle];
>
> NSString *timeString = [dateFormatter stringFromDate:[NSDate date]];
>
> [[self textField] setStringValue:timeString];
> }
>
> @end
>
>
_______________________________________________
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