Swift 当我的应用程序挂起或在后台时,我应该使计时器无效吗?
我有一个调度器,每15秒获取一次用户配置文件数据Swift 当我的应用程序挂起或在后台时,我应该使计时器无效吗?,swift,timer,background,Swift,Timer,Background,我有一个调度器,每15秒获取一次用户配置文件数据 class ProfileScheduler: NSObject { private var _timer: Timer? func start() { _timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(self.fetchProfile), userInfo: nil, repeats: true
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()
}
}
每次我的应用程序处于(挂起,背景)模式时,我是否应该使计时器无效?并在应用程序变为(活动)时将其还原
每次应用程序处于(挂起、后台)模式时,我是否应该使计时器失效
是。请参阅,这说明您应该停止计时器和其他定期任务。计时器需要一个活动的运行循环才能工作,所以当你的应用程序处于非活动状态时,你应该停止任何计时器,然后在它再次处于活动状态时重新启动它们