Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...