即使在后台Swift中如何处理计时器递减

问题描述

我正在从服务器获取当前日期和解锁日期。

unlockDate 2021-07-30 07:09:10 +0000
currentDate 2021-07-30 07:08:13 +0000


var remainingTime = Int()

     override func viewDidLoad() {
            super.viewDidLoad()
    
            let notificationCenter = NotificationCenter.default
            notificationCenter.addobserver(self,selector: #selector(appMovedToBackground),name: UIApplication.didEnterBackgroundNotification,object: nil)
            notificationCenter.addobserver(self,selector: #selector(appMovedToForground),name: UIApplication.willEnterForegroundNotification,object: nil)
    }
    
    
        func pauseCountDownTimer() {
            
            if countDownTimer != nil {
                self.countDownTimer?.invalidate()
            }
        }
        
        func restartCountDownTimer() {
            
             if countDownTimer != nil {
                self.startCountDownTimer()
            }
        }
    
     func startCountDownTimer() {
            
            let when = dispatchTime.Now() + 0.1
            dispatchQueue.main.asyncAfter(deadline: when) {
                let elapseTime = QuizModel().getUnlockCountDown(currentTime: self.currentTime ?? "",unlockTime: self.unlocksTime ?? "")
                self.remainingTime = Int(elapseTime)
                self.countDownTimer = .scheduledTimer(withTimeInterval: 1.0,repeats: true) { [weak self] _ in
                    self?.countDownTime()
                }
            }
        }

@objc func countDownTime() {
        
        self.remainingTime += 1
        self.unlockHrsLabel.text = "\(self.convertIntegerToFormat(remainingTime: self.remainingTime))"
        if remainingTime >= 0 {
            if self.isLockPopUpHidden == false {
                self.dismiss(animated: false,completion: nil)
            }
            self.stopTimer()
            self.callAPI()
        }
    }

 func getUnlockCountDown(currentTime: String,unlockTime: String) -> TimeInterval  {
        
        let dateFormatter = DateFormatter()
        let loc = Locale(identifier: "en_US_POSIX")
        dateFormatter.locale = loc
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        //  dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        let unlockDate = dateFormatter.date(from: "\(unlockTime)") ?? Date()
        let currentDate = dateFormatter.date(from: "\(currentTime)") ??  Date()
        
        let remaingTime = currentDate.timeIntervalSince(unlockDate)
        
       
        return remaingTime // -60 seconds
    }
    
    func convertIntegerToFormat(remainingTime: Int) -> String {
        
        let hours = abs(remainingTime / 3600)
        let seconds = abs(remainingTime % 60)
        let minutes = abs((remainingTime / 60) % 60)
        return (String(format: "%2d:%02d:%02d",hours,minutes,seconds))
        }

        func stopTimer(){
        if self.countDownTimer != nil {
            self.countDownTimer?.invalidate()
        }
    }

我的要求是无论设备时间和时区如何,计时器都应该运行。所以我实现了这一点。定时器达到0后,我必须调用一些api。

但是,当应用程序进入后台并进入前台时,计时器再次重新启动。 即使应用程序处于后台状态,如何递减计时器

我知道计时器不会在后台运行,但是,有没有办法处理这种情况。

有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)