0

I've got a 9 figure number that needs to be incremented by 500 each second, but i decided to increment the number each milliseconds and update the label that displays the number. I'm using a NSTimer but as i've read everywhere around they're not accurate nor meant to be. I've tried using CFAbsoluteTimeGetCurrent but couldn't get it to work. Simply using NSTimer yields and inaccurate value. The incrementation doesn't stop each time the user opens the app it simply adds up the value and starts incrementing again. Any Ideas?

Update:

Even though most answers point in the right direction, i solved my issue a little bit different. Thanks to all who answered and Martin.

I used a CADisplayLink instead of a NSTimer and got pretty accurate and constant results. Now notice i say pretty because the results are not totally accurate, but since in my case i'm incrementing a 9 figure number they're not noticeable, and my numbers are corrected as soon as the view appears again.

Sam Bing
  • 2,794
  • 1
  • 19
  • 29

4 Answers4

3

You can get Accuracy

timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(countup)userInfo:nil repeats:YES];
Narendra Mistri
  • 113
  • 2
  • 8
2

Use a timer only as a trigger to update the screen, do not rely on the exact time between each fire.

Keep an NSDate which represents the start time and use the current date when the timer fires to calculate the difference and update the label.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

Consider using CADisplayLink to update your interface — it gives you very accurate numbers of the time passed since the previous frame was drawn, so you can always keep your UILabel up to date, regardless of how high or low your framerate is.

Daniil Korotin
  • 720
  • 3
  • 14
0

The Time won't be very accurate - but it doesn't need to be, if you get the accurate current time each time you go into the loop and add 500 x (whole number of seconds), you will get a display that increases by 500 each second (plus / minus 50-100 milliseconds)

The advantage of this approach is that you won't get an ever-increasing discrepancy in the timing, only ever 50-100 milliseconds.

If you want the timer to stop when the user switches out of the app, then you need to disable the timer when the app becomes inactive - have a look at this tutorial on the Ray Wenderlich site http://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

Russell
  • 5,436
  • 2
  • 19
  • 27
  • Sure if i want to update the labels each second, the timer works. But i to display the label each millisecond, (similar to how a stopwatch increments). I want to increment the amount by 0.5 each millisecond and update the label. – Sam Bing Jan 02 '16 at 10:39
  • ok - missed that! You can still use the same sort of approach - fire the timer as quickly as you need, and use the accurate time to determine what to display You won't be able to see the counter display being updated every millisecond - how often do you actually need it to be updated? – Russell Jan 02 '16 at 10:48
  • As often as possible, sticking to the refresh rate of the screen would do. I must be missing something, but if i set the start time at the time the timer starts(app launch), my elapsed time will keep getting higher and higher right? So combined with the quick refresh rate it will take big leaps each time i run the function to update the labels. Im confused. – Sam Bing Jan 02 '16 at 11:04