Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Run Background Timer Swift

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()
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...