2

I'm trying to figure out how to use NSTimer, in my case I need to use it in real time updating every second kind of like a clock. I have read the documentation but, I'm not really sure how to use it. I've seen other posts on here talking about setting a timer instead of setting the timer to the real time and counting up in real time from there.

Question: How do I go about this?

2 Answers2

3

According to the docs, a NSTimer may be inaccurate, it fires when the system is idle enough to do so.

What you can do if are really dependant on exact time values: Create a timer and let it fire once a second. In the fired method ask for the exact system time and do your processing with it. This way you get always accurate time values independent of the accuracy of the timer events.


Some sample code: It stores the system time of the timer start. In the update method compute the exact time difference since timer start. So you get accurate values.

var timer: NSTimer?
var timerStart: NSDate?

func startTimer() {
    // get current system time
    self.timerStart = NSDate()

    // start the timer
    self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update:"), userInfo: nil, repeats: true)
}

func update() {
    // get the current system time
    let now = NSDate()

    // get the seconds since start
    let seconds = now.timeIntervalSinceDate(self.timerStart)

    ...
}
zisoft
  • 22,770
  • 10
  • 62
  • 73
  • I have a very vague idea on how to code this, could you help? –  Oct 31 '14 at 05:47
  • I have updated my answer and included some sample code. – zisoft Oct 31 '14 at 07:01
  • This may drift. Also, if you want to sample seconds you need to be polling at least 2 times a second. – Abizern Oct 31 '14 at 07:04
  • @Abizern: Sure, the polling interval depends on the OP's needs. But I cannot see a drift here when asking for the system time. – zisoft Oct 31 '14 at 07:08
  • This is exactly what I wrote in my answer, timers are inaccurate. That's the reason why I use the exact system time on each update. If you need exact intervals this is not the right approach. – zisoft Oct 31 '14 at 07:13
  • Where does it get the systems time here? –  Oct 31 '14 at 15:54
  • `NSDate()` gives you an object initialized with the current system date and time. – zisoft Oct 31 '14 at 16:00
  • When I call both functions in `viewDidLoad` it says its terminating with uncaught exception of type NSException and do I have to hook up anything to my Storyboard –  Oct 31 '14 at 16:07
  • @zisoft how do I call these functions? – alex Oct 31 '14 at 17:05
  • This is only a sample code which you can include in your class. There is no need to do something in your storyboard. Set breakpoints and step through your code to see where the error occurs and what error it is. – zisoft Oct 31 '14 at 17:08
  • does this solution is great for battery life ? :/ – Bénédicte Lagouge Jul 22 '19 at 14:38
3

dispatch_timers are more accurate and a better way to fire events every second. NSTimer has an accuracy of 50-100 milliseconds. This could be too much for real time (see here).

Community
  • 1
  • 1
Abizern
  • 146,289
  • 39
  • 203
  • 257