swift invalidate timer doesn't work

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.

  • UITableView DidSelectRowAtIndexPath?
  • How to completely Logout of Facebook SDK Auth in iOS App
  • PDFKitten is highlighting on wrong position
  • Warning: Implicit conversion loses Integer precision in xcode 6
  • How can I create different amount of uilabel in each uitableview row with uitableviewcell?
  • UIAlertView takes a long time to appear when called in a completion block
  • 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 😀

    Solutions Collect From Internet About “swift invalidate timer doesn't work”

    The usual way to start and stop a timer safely is

    var timer : NSTimer?
    
    func startTimer()
    {
      if timer == nil {
        timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: "timerFired", userInfo: nil, repeats: true)
      }
    }
    
    func stopTimer()
    {
      if timer != nil {
        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.

    In Swift 3 replace

    • NSTimer with Timer,
    • NSTimer.scheduledTimerWithTimeInterval( with Timer.scheduledTimer(timeInterval:
    • selector: "timerFired" with selector: #selector(timerFired).