1
votes

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.

2
From the docs: A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. - spassas
Your problem could be related to the code run affects the timer if it does not finish calculations before firing again. - Sverrisson
Thanks for the comments.I found the problem. I should use 100 instead of 60 for 0.01. and I think thats the big issue. I dont need this timer to be very accurate. Thanks tho. - martingale

2 Answers

0
votes

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.

0
votes

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.