Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the minimum valid time interval of an NSTimer?

I want to use NSTimer to increase the number which show on a label.

Here is my code:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 90, 90, 30)];
[self.view addSubview:self.numberLabel];


self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(refreshText) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)refreshText{

NSDate *beginDate = [NSDate date];

static NSInteger a = 0;
a ++;

self.numberLabel.text = [NSString stringWithFormat:@"%ld",a];

if (a == 1000) {

    NSDate *endDate = [NSDate date];

    NSTimeInterval durationTime = [endDate timeIntervalSinceDate:beginDate];
    NSTimeInterval intervalTime = self.timer.timeInterval;

    NSLog(@"durationTime = %f",durationTime);
    NSLog(@"intervalTime = %f",intervalTime);

    [self.timer invalidate];
    self.timer = nil;
} 
}

and the console showed: enter image description here

then I changed the timer's timeInterval from 0.01 to 0.001,the console showed: enter image description here

What confused me is that why the durationTime is not 0.0000056 when the timeInterval is 0.001.What's more,is there a min value for NSTimer's timeInterval we can set?

like image 674
无夜之星辰 Avatar asked Mar 08 '23 18:03

无夜之星辰


1 Answers

The time period of an NSTimer is a value of type NSTimeInterval, while this provides sub-millisecond precision that does not help you. From the start of the NSTimer documentation:

Timers work in conjunction with run loops. Run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.

To use a timer effectively, you should be aware of how run loops operate. See Threading Programming Guide for more information.

A timer is not a real-time mechanism. If a timer’s firing time occurs during a long run loop callout or while the run loop is in a mode that isn't monitoring the timer, the timer doesn't fire until the next time the run loop checks the timer. Therefore, the actual time at which a timer fires can be significantly later. See also Timer Tolerance.

So the minimum time interval for an NSTimer is tied to the the length of a run loop iteration. While internal optimisations, if they exist, could fire a timer as soon as it is set if the interval is really small in general the shortest period you'll get is dependent on the remaining execution time of the run loop iteration in which the timer is set, which is pretty much indeterminate for general purpose programming.

If you really need a high-resolution timer (see @bbum's comment on your question) then you'll need to research that topic - just search something like "high resolution timing macOS" as a starting point.

HTH

like image 200
CRD Avatar answered Mar 11 '23 07:03

CRD