简体   繁体   English

如何完全删除NSTimer ScheduledTimerWithTimeInterval

[英]How to remove NSTimer scheduledTimerWithTimeInterval entirely

I wave a time value inside NSUserDefaults , and when I'm reusing my ViewController my NSTimer adds +1 to Interval 我在NSUserDefaults内部挥动一个时间值,当我重用ViewController我的NSTimer将+1添加到Interval

here my update function: 这是我的更新功能:

func fireUpdatedLimit() {
        var endTimeSeconds = userDefaults.integerForKey("endTimeSeconds\(currentUserId!)")
        if endTimeSeconds > 0  {
            endTimeSeconds += 1
            userDefaults.setInteger(endTimeSeconds, forKey: "endTimeSeconds\(currentUserId!)")
        }
        self.timer =  NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.updateLimit), userInfo: nil, repeats: true)
    }

and here what I've already tried: 这是我已经尝试过的:

weak var timer = NSTimer() 
weak var timer: NSTimer?

self.timer = nil 
self.timer.invalidate()

logic: 逻辑:

func updateLimit() {

        var timeLeft = userDefaults.integerForKey("endTimeSeconds\(currentUserId!)")
        foregroundAction() {
            self.lockedTimeLabel.hidden = false
            self.lockedImageView.hidden = false
        }
        self.lockedTimeLabel.text = String(fromTimeInterval: Double(timeLeft))
        if timeLeft <= 0 {
            print("ended")
            foregroundAction() {
                self.lockedImageView.hidden = true
                self.lockedTimeLabel.hidden = true
                self.mediaImageView.hidden = true
                self.followerImageView.hidden = true
            }
            self.reloadContent()
            userDefaults.setInteger(timeLeft, forKey: "endTimeSeconds\(currentUserId!)")
            self.timer!.invalidate()
        } else {
            timeLeft -= 1

            userDefaults.setInteger(timeLeft, forKey: "endTimeSeconds\(currentUserId!)")
        }
    }

If you set the timer to nil then the reference is broken and you can't invalidate it, so you must call invalidate first. 如果将计时器设置为nil则引用将被破坏并且无法使其无效,因此必须首先调用invalidate

Ensure that you always check the reference and invalidate when you're going to create a new timer as that also breaks any reference to the old timer. 确保要创建新计时器时始终检查引用并使其无效,因为这也会破坏对旧计时器的任何引用。

Also, never create a dummy / placeholder timer with Timer() , you require an optional reference ( ? ). 另外,永远不要使用Timer()创建一个虚拟/占位符计时器,您需要一个可选的引用( ? )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何停止NSTimer.scheduledTimerWithTimeInterval - How to stop NSTimer.scheduledTimerWithTimeInterval NSTimer ScheduledTimerWithTimeInterval-不调用功能 - NSTimer scheduledTimerWithTimeInterval - not calling funciton 自动锁定ios设备,NSTimer和scheduledTimerWithTimeInterval - autolocking ios device, NSTimer and scheduledTimerWithTimeInterval 当按下主页按钮时,如何在spritekit [SWIFT]中暂停和重新启动NSTimer.scheduledTimerWithTimeInterval? - How to pause and restart NSTimer.scheduledTimerWithTimeInterval in spritekit [SWIFT] when home button is pressed? iOS 9兼容版NSTimer scheduledTimerWithTimeInterval:重复:阻止:? - iOS 9 compatible version of NSTimer scheduledTimerWithTimeInterval:repeats:block:? 在Objective-C中为scheduledTimerWithTimeInterval反转NSTimer - Reverse NSTimer for scheduledTimerWithTimeInterval in Objective-C dispatch_after 递归 vs NSTimer scheduledtimerwithtimeinterval - dispatch_after recursion vs NSTimer scheduledtimerwithtimeinterval 与ScheduledTimerWithTimeInterval添加时NSTimer不触发选择器 - NSTimer not firing selector when added with scheduledTimerWithTimeInterval Swift的NSTimer.scheduledTimerWithTimeInterval的两个参数集 - Two parameter sets for Swift's NSTimer.scheduledTimerWithTimeInterval 无法识别的选择器使用NSTimer.scheduledTimerWithTimeInterval函数发送到实例 - Unrecognized selector sent to instance with NSTimer.scheduledTimerWithTimeInterval function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM