Run Background Timer Swift

2019-09-19 23:31发布

问题:

I solved the problem.

var backgroundUpdateTask: UIBackgroundTaskIdentifier = 0

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    return true
}

func applicationWillResignActive(application: UIApplication) {
    self.backgroundUpdateTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
        self.endBackgroundUpdateTask()
    })
}

func endBackgroundUpdateTask() {
    UIApplication.sharedApplication().endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
}

func applicationWillEnterForeground(application: UIApplication) {
    self.endBackgroundUpdateTask()
}

Timer doesn't work until it's activated. I want to do the timer update process even when the application is in the background. I update the current time with the tick function and when the timer is synchronized with the power on / off timer, I do the turn on or off. I want timer and the update process to work in the background.

override func viewDidLoad()
    {
        super.viewDidLoad()
 timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(self.tick) , userInfo: nil, repeats: true)
}

@objc func tick() {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/yyyy HH:mm"

        labelTimer.text = formatter.string(from: Date())

        zaman()
        zaman1()
    }

@objc func zaman(){

        if timertext.text == String? (labelTimer.text!) {
            zamanlayıcıfunc()
        }else{
return
        }
    }
    @objc func zamanlayıcıfunc()
    {

            if labelcheckbox.text == ("aç"){
                updateState()

            }
           if labelcheckbox.text == ("kapat"){
                updateState1()
            }

        }


    @objc func zaman1(){

        if timertext2.text == String? (labelTimer.text!) {
            zamanlayıcıfunc1()
        }else{
            return
        }
    }
    @objc func zamanlayıcıfunc1()
    {

        if labelcheckbox2.text == ("saatinde kapat"){
            updateState1()

        }
        else{
            updateState()
        }

    }

 @objc func updateState(){


            let ref = Database.database().reference()
            ref.child("\(chip1InfoString1!)/states/\(self.ekle2.text!)").setValue(true)
getData()
    }

回答1:

Your application will not continue processing in the background. If all applications could do that the phone battery would easily and quickly be drained.

The OS will provide you with some limited background execution time, but if you use up too many resources it will be further limited. You can read more about it in Apple's documentation.

What you may need to do is keep track of when the app went into the background and foreground, using UIApplication.didEnterBackgroundNotification and UIApplication.willEnterForegroundNotification, to see how much time has passed.



标签: swift timer