Creating a new instance of Timer when app enters foreground?

左心房为你撑大大i 提交于 2019-12-25 04:14:10

问题


I am creating an app where a timer is set by the user. When the app goes to the background, the timer.invalidate(). Now I want the timer to start again when the app comes back to the foreground. I am creating another instance of timer to do it when the app sends notification that app is in the foreground. However, it's not firing the function.

In Viewdidload() I am creating a timer:

 timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleCountdown), userInfo: nil, repeats: true)
    RunLoop.current.add(self.timer!, forMode: RunLoop.Mode.common)

And then I have notifications that check if the app is in background or in foreground:

When it enters background I am invalidating the timer.

  @objc func applicationDidEnterBackground() {
    let defaults = UserDefaults.standard
    let quitTime = Date()
    defaults.set(quitTime, forKey: "quitTimeKey") //Storing the time of quit in UserDefaults
    timer?.invalidate()

}

When the app gets back out, I first check if the timer is isValid or not, and then create a new timer. But this timer doesn't seem to work.

@objc func appEntersForeground() {
    calculateTimeLeft()
    if let timer = timer {
        if (!timer.isValid)
        {
            Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleCountdown), userInfo: nil, repeats: true)
        }
    }

}

Some help here will be appreciated!


回答1:


Declare your timer property as weak:

weak var timer: Timer?

Then it will be set to nil when the timer is invalidated. Then just check if timer is nil before creating a new one:

@objc func appEntersForeground() {
    calculateTimeLeft()
    if timer == nil {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleCountdown), userInfo: nil, repeats: true)
    }
}


来源:https://stackoverflow.com/questions/51762833/creating-a-new-instance-of-timer-when-app-enters-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!