This timer isn't firing every second, when I check the log and UI it seems to be firing every 3-4 seconds.
func startTimer() {
print("start timer")
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(timerDidFire),
userInfo: nil,
repeats: true)
}
func timerDidFire(timer: Timer) {
print("timer")
updateLabels()
}
Is this just something that is going to happen on the Watch due to lack of capabilities, or is there something wrong in my code?
Here is the log if needed:
0.0396000146865845
3.99404102563858
7.97501903772354
11.9065310359001
EDIT:
And for clarification, what I'm updating every second is the workout timer, so it needs to be updated every second that ticks by.
Consider using a WKInterfaceTimer label in place of the label that you are using to show the timing:
A WKInterfaceTimer object is a special type of label that displays a countdown or count-up timer. Use a timer object to configure the amount of time and the appearance of the timer text. When you start the timer, WatchKit updates the displayed text automatically on the user’s Apple Watch without further interactions from your extension. Apple Docs.
WatchOS will then take responsibility for keeping this up-to-date. The OS handles the label for you, but you have to keep track of the elapsed time: you just need to set an NSDate to do that (see example below).
Sample Code.
In your WKInterfaceController subclass:
// Hook up a reference to the timer. @IBOutlet var workoutTimer: WKInterfaceTimer! // Keep track of the time the workout started. var workoutStartTime: NSDate? func startWorkout() { // To count up use 0.0 or less, otherwise the timer counts down. workoutTimer.setDate(NSDate(timeIntervalSinceNow: 0.0)) workoutTimer.start() self.workoutStartTime = NSDate() } func stopWorkout() { workoutTimer.stop() } func workoutSecondsElapsed() -> NSTimeInterval? { // If the timer hasn't been started then return nil guard let startTime = self.workoutStartTime else { return nil } // Time intervals from past dates are negative, so // multiply by -1 to get the elapsed time. return -1.0 * self.startTime.timeIntervalSinceNow }
Comprehensive blog entry: here.