0

The Timer on WatchKit doesn't execute the lblTimer.setText("(currentTime)") properly when it is there in the function called when the timer fires. What should I do to fix this problem? The label is just static and should be updated very often. Does the watchOS simulator have a bug?

Also, I tried the Timer from watckKit and that didn't work either.

Here's the sample code where the lblTimer.setText doesn't execute on the screen:

@objc func fire()
{
    print(countDown)
    if (countDown <= 0.0) {
        timer?.invalidate()
        speechSynthesizer?.speak(AVSpeechUtterance(string: "You ran out of time! Next question!"))
        countDown = 30.0
        lblTimer.setText("30.0")
    } else {
        countDown -= 0.01

        let numberOfPlaces = 2.0
        let multiplier: Float = Float(pow(10.0, numberOfPlaces))
        let num: Float = countDown
        let rounded = round(num * multiplier) / multiplier
        lblTimer.setText("Time: \(rounded)")
        print(rounded)
    }

}
  • Show your code. I just tried a Timer label (`WKInterfaceTimer`) and it ticks away when I call `.start()` on it. Similarly, updating a conventional label (a `WKInterfaceLabel`) from inside a `Timer.scheduledTimer(....)` works fine too. – Chris Sep 26 '19 at 16:24
  • I just did @Chris – Shyamal Chandra Sep 26 '19 at 19:04
  • It sounds like your timer `fire()` is probably not being called on the main thread. Try `DispatchQueue.main.sync { lblTimer.setText("Time: \(rounded)" }`. Also, see this related question/answer : https://stackoverflow.com/a/39066029/325366 – Chris Sep 27 '19 at 11:22
  • Hi @Chris, the timer `fire()` is being called but when I put the `DispatchQueue.main.sync`, there is an assertion fault during runtime with the following error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) – Shyamal Chandra Sep 27 '19 at 13:42

0 Answers0