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).
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.