简体   繁体   中英

timer runs quicker on simulation device iOS?

I have an timer app. It works fine on the Mac simulator. But when I downloaded to my iPhone 6s for device simulation. The timer actually runs quicker than normal, its like twice quicker than it should be. What is the problem here?

timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "result", userInfo: nil, repeats: true)

So the 0.01 means 1/100 second or 1/60 second? That is confusing me.

Timers in computers are not 100% accurate anyway for small intervals

Keep these points in mind :

1-

You have a timer that will execute roughly 100 times per second (interval of 0.01).

2-

NSTimer is not that accurate, you probably won't get it to work every 1 ms.

3-

It is fired from the run loop so there is latency and jitter.

Also note that your timer is highly inaccurate. The timer will not repeat EXACTLY every hundredth of a second. It may only run 80 times per second or some other inexact rate.

Try using GCD calls.

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, <#dispatchQueue#>);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, <#intervalInSeconds#> * NSEC_PER_SEC, <#leewayInSeconds#> * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
    <#code to be executed when timer fires#>
});
dispatch_resume(timer);

Here you can specify the timer interval in nanoseconds if you want finder granularity. But as mentioned in another comment its hard to get the accurate callback rate due to various factors of the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM