SwiftUI - Displaying a countdown timer is interrupting UI navigation

Hi there,

My SwiftUI project displays a cooldown timer to inform a user when they can attempt a new data download.

This timer is being initiated after finishing a data fetch and is working on the main thread of a dispatch group:

Code Block
dispatchGroupForDownloads.notify(queue: .main) {
            self.cooldowncounter = 10
            if self.timer == nil { self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateCounter), userInfo: nil, repeats: true)}


My updateCounter method decrements the timer:

Code Block
    @objc func updateCounter() {
        if cooldowncounter > 0 {
            cooldowncounter -= 1
        } else if cooldowncounter == 0 {
            timer?.invalidate()
            timer = nil
}
    }


The timer works as expected and is displayed to the user. However, this is the issue: Whenever the timer is counting down, I cannot navigate anywhere else in my app without it "snapping back" to the previous view. That's the best way I can describe it.

Is there any other way to show a timer to the user that does not interrupt the UI?

SwiftUI - Displaying a countdown timer is interrupting UI navigation
 
 
Q