NSTimer timestamp timeinterval question
I have the following code:
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerCount:) userInfo:n开发者_如何转开发il repeats:YES]; -(void)timerCount:(NSTimer *)timer { NSTimeInterval dt = [timer timeInterval]; // do something }
The NSTimeInterval I got will be 0.5, the time interval I've put on scheduledTimerWithInterval, this means the timerCount will be called each 0.5 seconds.
But I now that there are some stuff as timeStamps, and I want to know if the NSTimer will call the timerCount method in PRECISELY 0.5 seconds each time.
You won't get that using NSTimer on main thread as the timer is only called by event loop.
If you need maximal precision just create a thread with it's own message loop and schedule the timer there.
aTimer = [NSTimer timerWithTimeInterval:(1.0) target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:aTimer forMode: NSDefaultRunLoopMode];
- (void)timerFired:(NSTimer*)theTimer
{
if(condition)
{
//timer terminated
[theTimer isinValid];
}
}
精彩评论