Comments on: #9 How to invalidate NSTimer properly? http://swifting.io/blog/2016/03/13/9-how-to-invalidate-nstimer-properly/ Sharing passion in Swift Thu, 06 Jun 2019 03:01:16 +0000 hourly 1 https://wordpress.org/?v=5.2.4 By: Blade http://swifting.io/blog/2016/03/13/9-how-to-invalidate-nstimer-properly/#comment-297 Thu, 06 Jun 2019 03:01:16 +0000 http://swifting.io/?p=316#comment-297 Solution #2 should work well in theory, however, in my following code, it doesn’t seem to have any effect. Any ideas what I might be doing wrong?. The line “Counter deinited” is never printed. (Btw, this is a command-line project)

import Foundation

class IncrementingCounter {

private class TimerTargetWrapper {
weak var target: IncrementingCounter?
init(target: IncrementingCounter?) {
self.target = target
}
@objc func tick(timer: Timer) {
target?.incrementCounter()
}
}

private var counter = 1
private var timer: Timer?

deinit {
print(“Counter deinited”)
timer?.invalidate()
}

func start() {
print(“Starting timer”)
guard timer == nil else { return }
timer = Timer.scheduledTimer(timeInterval: 1,
target: TimerTargetWrapper(target: self),
selector: #selector(TimerTargetWrapper.tick(timer:)),
userInfo: nil,
repeats: true)
print(“Scheduled timer”)
}

@objc private func incrementCounter() {
print(counter)
counter += 1
}
}

var counter: IncrementingCounter? = IncrementingCounter()
counter?.start()

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) { [weak counter] in
print(“Setting counter to nil”)
counter = nil
}

CFRunLoopRun()

]]>
By: Kamlesh http://swifting.io/blog/2016/03/13/9-how-to-invalidate-nstimer-properly/#comment-240 Fri, 24 Aug 2018 02:33:58 +0000 http://swifting.io/?p=316#comment-240 Thanks, Solution #2 worked well for me.

]]>
By: Michał Wojtysiak http://swifting.io/blog/2016/03/13/9-how-to-invalidate-nstimer-properly/#comment-18 Tue, 15 Mar 2016 20:21:09 +0000 http://swifting.io/?p=316#comment-18 Hi Jeff. Very good point. Abstracting timer functionality, using wrapper class, would bring many benefits for unit testing. Thank you!

Best regards,
swifting.io

]]>
By: Jeff Gilbert http://swifting.io/blog/2016/03/13/9-how-to-invalidate-nstimer-properly/#comment-16 Mon, 14 Mar 2016 16:13:40 +0000 http://swifting.io/?p=316#comment-16 I like that you show how the first instinct for a solution might not be the best solution.

You might also consider not having your interactor dependent on a concrete class like NSTimer, but rather an abstract role like an Updater. By using a test updater (that you control) in your tests you can ensure the interactor responds properly when it is told to update its content. This means your test can execute in milliseconds, rather than 15 seconds.

How/when an update is triggered is outside the responsibility of the interactor. In your production code, you could have a TimedUpdater that uses NSTimer to determine when to trigger an update.

]]>
By: abc http://swifting.io/blog/2016/03/13/9-how-to-invalidate-nstimer-properly/#comment-15 Mon, 14 Mar 2016 09:57:57 +0000 http://swifting.io/?p=316#comment-15 https://gist.github.com/nixzhu/983cedfd05afe9401ba5
maybe this help

]]>