当我的应用程序暂停或在后台运行时,我应该使计时器无效吗? [英] Should I invalidate timer when my application is suspended or in background?

查看:104
本文介绍了当我的应用程序暂停或在后台运行时,我应该使计时器无效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调度程序,它每15秒获取一次用户个人资料数据.

I have scheduler that fetch user profile data every 15 seconds.

class ProfileScheduler: NSObject {

    private var _timer: Timer?

    func start() {
        _timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(self.fetchProfile), userInfo: nil, repeats: true)
    }

    func fetchProfile() {
       print("Update........")
       ProfileManager.getProfileData()
    }

}

我每次以(挂起,后台)模式运行应用程序时,都应该使Timer失效吗?并在应用变为(活动)时将其恢复.

Should I invalidate Timer each time my application in (Suspended, Background) mode? And restore it when app become (Active).

推荐答案

我每次在(暂停,后台)模式下运行应用程序时,都应该使Timer无效吗?

Should I invalidate Timer each time my application in (Suspended, Background) mode?

.请参见

Yes. See What to do When Your App is Interrupted Temporarily, which explains that you should stop timers and other periodic tasks. Timers need an active run loop to work, so you should stop any timers when your app becomes inactive, and then restart them when it becomes active again.

这篇关于当我的应用程序暂停或在后台运行时,我应该使计时器无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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