swift invalidate timer doesn't work

眉间皱痕 提交于 2019-12-17 20:05:03

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/34507799/swift-invalidate-timer-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!