So I am stuck I am building a bingo app and I want the app to stop the timer when the app goes into the background. my code looks like this
var timer = Timer()
func ViewDidLoad(){
// other stuff
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(numberPicker), userInfo: nil, repeats: true)
}
In other posts I've read about this I've seen people recommend defining timer as a weak variable. When I try this My app crashes when it try's to make the timer.
A modern solution is a DispatchSourceTimer
which can be suspended and resumed.
The two notification observers use also modern swifty block based API, no @objc
needed.
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
override func viewDidLoad() {
super.viewDidLoad()
timer.schedule(deadline: .now() + .seconds(5), repeating: 5.0)
timer.setEventHandler {
print("Timer fired")
}
NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: .main) { [weak self] _ in
self?.timer.suspend()
}
NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { [weak self] _ in
self?.timer.resume()
}
timer.activate()
}