• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Observing Time
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Observing Time


  • Subject: Re: Observing Time
  • From: Andy Lee <email@hidden>
  • Date: Sat, 27 Jul 2013 22:28:40 -0400

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


  • Follow-Ups:
    • Re: Observing Time
      • From: email@hidden
References: 
 >Observing Time (From: email@hidden)
 >Re: Observing Time (From: Rick Mann <email@hidden>)
 >Re: Observing Time (From: email@hidden)
 >Re: Observing Time (From: Ken Thomases <email@hidden>)
 >Re: Observing Time (From: email@hidden)
 >Re: Observing Time (From: Ken Thomases <email@hidden>)
 >Re: Observing Time (From: email@hidden)
 >Re: Observing Time (From: Ken Thomases <email@hidden>)
 >Re: Observing Time (From: email@hidden)
 >Re: Observing Time (From: Scott Ribe <email@hidden>)
 >Re: Observing Time (From: email@hidden)
 >Re: Observing Time (From: Kyle Sluder <email@hidden>)
 >Re: Observing Time (From: email@hidden)
 >Re: Observing Time (From: Scott Ribe <email@hidden>)
 >Re: Observing Time (From: email@hidden)
 >Re: Observing Time (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: Observing Time
  • Next by Date: Re: Observing Time
  • Previous by thread: Re: Observing Time
  • Next by thread: Re: Observing Time
  • Index(es):
    • Date
    • Thread