NSTimer accuracy
NSTimer accuracy
- Subject: NSTimer accuracy
- From: Mike Laster <email@hidden>
- Date: Mon, 18 Nov 2002 12:47:03 -0500
If I have a timer scheduled to fire at a particular time, is it
possible for Foundation to trigger it early? I'm running into an
intermittent bug with a server which needs to run a method every
midnight. The problem is that sometimes I see the event happen twice
at nearly the same instant. The method is not thread-safe, since only
one is supposed to be running at a time, and it tends to crash the
server.
Maybe I just need a different paradigm for supporting "run this method
every midnight". What I currently do is:
- (void) timerTick:(NSTimer *)inTimer
{
NSCalendarDate *alarmTime = nil;
NSTimer *timer = nil;
alarmTime = [[[NSCalendarDate date] beginningOfDay]
dateByAddingDays:1];
[NSThread detachNewThreadSelector:@selector(doDailyStuff:)
toTarget:self
withObject:nil];
timer = [NSTimer scheduledTimerWithTimeInterval:[alarmTime
timeIntervalSinceNow]
target:self selector:_cmd
userInfo:[inTimer userInfo]
repeats:NO];
}
beginningofDay: is a category on NSCalendarDate of:
- (NSCalendarDate *) beginningOfDay
{
int year = [self yearOfCommonEra];
int month = [self monthOfYear];
int day = [self dayOfMonth];
NSTimeZone *tz = [self timeZone];
return [NSCalendarDate dateWithYear:year month:month day:day
hour:0 minute:0 second:0
timeZone:tz];
}
alarmTime should be "the next nearest midnight in the future". The
problem is that if timerTick: fires at 11:59:59 that means "in one
second", which is
not my intent.
Is there a better way of going about this?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.