运行后台计时器 Swift [英] Run Background Timer Swift

查看:53
本文介绍了运行后台计时器 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解决了这个问题.

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()
    }

推荐答案

您的应用程序将不会在后台继续处理.如果所有应用程序都能做到这一点,手机电池将很容易快速耗尽.

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

操作系统将为您提供一些有限的后台执行时间,但如果您使用太多资源,它将进一步受到限制.您可以在 在 Apple 文档中阅读更多相关信息.

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.

您可能需要做的是跟踪应用何时进入后台和前台,使用 UIApplication.didEnterBackgroundNotificationUIApplication.willEnterForegroundNotification,看看有多少时间过去了.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆