I have to do heavy operation every < 1s. I'm using NSTimer, but its not that accurate as i expected... I'm using 2 timers. One to update data in my model and 2nd to update my views (few labels and custom 'fancy-circle' progress bar)
Ok... my code:
_valueTimer =
[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(counterTask)
userInfo:nil
repeats:YES];
_progressTimer =
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(updateViews) // heavy stuff here
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_progressTimer
forMode:NSRunLoopCommonModes];
As you can see _progressTimer
selector is run every 0.1 sec.
One of the views is my HH:MM:SS time (data from first timer!). It display different time than real time when my progressbar is updating (heavy operation) -> 10 sec in app == 12 sec in real time... Its too much difference. When I comment my progress bar update - it all works correctly
Could you tell me how to force my timer to run exactly after my interval? Or skip one cycle when its too much for it to handle? The most important thing is to not slow down...
Thanks
Ok. I found solution. Maybe not perfect, but it works.
In counterTask
method instead of increasing my _actualTime
value by 0.01... I calculate real interval between 'ticks' like this:
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
startDate = currentDate;
_actualTime += timeInterval;