I have 4 NSTimers
objects in my app , that make requests to a rest URL every few seconds.
On clicking of a particular button
I want to stop the timer so that it stops polling and on click of another button
I want to resume polling.
I have tried invalidate
for all timers but does not work.
NOTE: All timers are in different class and I'm trying to invalidate the timers in another class Any help will be appreciated , thank you.
class NotificationViewController:UIViewController {
var timer:NSTimer?
getEvents(){
if((timer == nil)) {
timer = NSTimer.scheduledTimerWithTimeInterval(20.0, target: self, selector: Selector("getEvents"), userInfo: nil, repeats: true)
}
}
}
In another class I'm doing this on click of a button
class Menu:UIViewController {
@IBAction func buttonTapped(sender: UIButton) {
self.notify.timer?.invalidate()
self.notify.timer = nil
}
}
It seems that you are losing the original reference to the timers. One possible evil solution, is to keep the reference across the app. You can do that using
struct
:This way you could access the timer from anywhere in the program:
Cancelling the timer:
if (timer.isValid()) { timer.invalidate() }
timer = nil;
Just debug you app and check where you are not releasing the timer.
Bear in mind, that timer retains it's target, so if you want to release the target, you will need to release the timer as well, or you can write your own timer with week reference to the target.
Try to create a separate
NotificationTimer
class and used its shared object all over the project like this way.Now call this
timer1
object inside anyViewController
like this way.Or call that method like this way