Should I invalidate timer when my application is s

2019-06-03 00:45发布

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

}

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

1条回答
Bombasti
2楼-- · 2019-06-03 01:34

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.

查看更多
登录 后发表回答