Trouble with NSTimer and RunLoop on macOS 13.6 causing UI to freeze intermittently
I'm deploying to production and I'm working on a personal project and I'm experiencing intermittent UI freezes in my macOS application that uses an NSTimer to update a progress bar. The timer is set to fire every second, but I've noticed that sometimes the UI becomes unresponsive for a short period, especially when the timer fires during heavy processing in the main thread. I tried moving the timer to a background thread, but that caused issues with updating the UI since all UI updates must happen on the main thread. Here's the code snippet I currently have for setting up the timer: ```swift var progressTimer: Timer? func startTimer() { progressTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true) RunLoop.main.add(progressTimer!, forMode: .common) } @objc func updateProgress() { // Simulating heavy processing for _ in 0..<1000000 { // Some CPU intensive task } progressBar.increment(by: 1) } ``` I've tried wrapping the heavy processing part inside a background dispatch queue like this: ```swift dispatchQueue.global(qos: .background).async { // Simulating heavy processing for _ in 0..<1000000 { // Some CPU intensive task } DispatchQueue.main.async { self.progressBar.increment(by: 1) } } ``` However, this approach doesn't seem to resolve the issue, as the UI still freezes briefly. I suspect I might not be managing the timer correctly or that there's another issue at play. Has anyone else faced similar issues with NSTimer on macOS 13.6, and how did you resolve it? This is my first time working with Swift 3.10. Could this be a known issue?