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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…