I am creating an macOS application with a NSTimer instance in the background using this class:
class RepeatingTimer {
private var timer: Timer?
init(timeInterval: TimeInterval, eventHandler: @escaping ((Timer) -> Void)) {
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true, block: eventHandler)
}
deinit {
cancel()
}
func cancel() {
timer?.invalidate()
}
}
Except when I terminate my app (CMD+Q) or just regularly quit my app, there is still a process of my app in Activity Monitor, event though I terminate the NSTimer in AppDelegate:
func applicationWillTerminate(_ aNotification: Notification) {
MainVC.repeatingTimer?.cancel()
}
I setup my timer in the main viewDidLoad()
method of my application like this:
MainVC.repeatingTimer = RepeatingTimer(timeInterval: TimeInterval(1), eventHandler: onTimerUpdate(timer:))
I also tried to delegate my main ViewController with NSWindowDelegate, and use this to try to get rid of the process, but that did also not work:
override func viewDidAppear() {
self.view.window?.delegate = self
}
func windowWillClose(_ notification: Notification) {
MainVC.repeatingTimer?.cancel()
}
com.apple.NSEventThread
running, plus another Thread with0 0x00000000
as instruction. – DevxlnapplicationWillTerminate()
actually get called? If you break in there, does the debugger stop? – user1118321