Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift invalidate timer doesn't work

Tags:

ios

swift

nstimer

I have this problem for a few days now and I don't get what I am doing wrong.

My application is basically just creating some timers. I need to stop them and create new ones. But at the moment stopping them doesn't work.

self.timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target:self, selector: "timerDidEnd:", userInfo: "Notification fired", repeats: false)

That's my timer

func timerDidEnd(timer:NSTimer){
    createUnrepeatedAlarmWithUpdateInterval()
}

Because my timer didn't want to stop I am currently using the unrepeated timer and start it myself after it stopped.

func stopAlarm() {

    if self.timer != nil {
        self.timer!.invalidate()
    }
    self.timer = nil
    self.timer = NSTimer()
}

And that's how I stop my timer.

alarmManager.stopAlarm()
alarmManager.createUnrepeatedAlarmWithUpdateInterval()

I call the stopAlarm() function before creating a new timer.

I really don't know what I am doing wrong so I appreciate every answer :)

class AlarmManager: ViewController{

private var timer : NSTimer?
private var unrepeatedTimer : NSTimer?
private let notificationManager = NotificationManager()
private var current = NSThread()
private let settingsViewController = SettingsViewController()

func createRepeatedAlarmWithUpdateInterval(){

    var timeInterval:NSTimeInterval = settingsViewController.getUpdateIntervalSettings()

    if timer == nil{
    timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
        target: self,
        selector: "repeatedTimerDidEnd:",
        userInfo: "Notification fired",
        repeats: true)
    }
}
func repeatedTimerDidEnd(repeatedTimer:NSTimer){
    ConnectionManager.sharedInstance.loadTrainings(settingsViewController.getServerSettings())
    createUnrepeatedAlarm(10)
}

func createUnrepeatedAlarm(timeInterval:Double){

    unrepeatedTimer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
        target: self,
        selector: "unrepeatedTimerDidEnd:",
        userInfo: "Notification fired",
        repeats: false)
}
func unrepeatedTimerDidEnd(unrepeatedTimer:NSTimer){
    notificationManager.createNotification(self, reminderType: NotificationManager.ITEMRATINGREMINDER)
    notificationManager.createNotification(self, reminderType: NotificationManager.ITEMREMINDER)
    print("UnrepeatedAlarm ended")
}

func stopAlarm(){
    print("StopAlarm triggered")
    if (timer != nil)
    {
        print("stoptimer executed")
        timer!.invalidate()
        timer = nil
    }

    if (unrepeatedTimer != nil)
    {
        unrepeatedTimer!.invalidate()
        unrepeatedTimer = nil
    }
}
}

Thats the whole code of this class. Maybe that helps :D

like image 579
Banelu Avatar asked Dec 29 '15 09:12

Banelu


3 Answers

The usual way to start and stop a timer safely is

var timer : Timer?

func startTimer()
{
  if timer == nil {
    timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(timerFired), userInfo: nil, repeats: true)
  }
}

func stopTimer()
{
    timer?.invalidate()
    timer = nil
}

startTimer() starts the timer only if it's nil and stopTimer() stops it only if it's not nil.

You have only to take care of stopping the timer before creating/starting a new one.

like image 107
vadian Avatar answered Nov 14 '22 11:11

vadian


Make sure you're calling invalidate on the same thread as the timer.

From the documentation:

Special Considerations You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

https://developer.apple.com/documentation/foundation/nstimer/1415405-invalidate?language=objc

like image 15
Kevin Kruusi Avatar answered Nov 14 '22 09:11

Kevin Kruusi


Something that's not really covered by the previous answers is that you should be careful your timer isn't scheduled multiple times.

If you schedule a timer multiple times without first invalidating it, it'll end up scheduled on multiple run loops, and invalidating it then becomes nigh impossible.

For me, it happened when calling my scheduleTimer() function in separate functions in my view controller's life cycle (viewWillAppear, viewDidAppear, ...)

So in short, if you aren't sure (or you cannot guarantee) your Timer is only scheduled once, just always invalidate it first.

like image 4
Skwiggs Avatar answered Nov 14 '22 09:11

Skwiggs