Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created June 24, 2022 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/f769086620f10d9319f67e282429e8d3 to your computer and use it in GitHub Desktop.
Save robertmryan/f769086620f10d9319f67e282429e8d3 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
@IBOutlet var label: UILabel!
private weak var timer: Timer?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
startTimer()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
stopTimer()
}
func startTimer() {
stopTimer() // stop previous timer, if any
var counter = 0
timer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { [weak self] timer in
counter += 1
guard let self = self, counter <= 10 else {
timer.invalidate()
return
}
let string = String(counter) // was Int.random(in: 1...10), but wanted to see counter progress
print(string)
self.label.text = string
}
}
func stopTimer() {
timer?.invalidate()
}
}
@robertmryan
Copy link
Author

A few observations on the above:

  1. When starting timer in viewDidAppear, you’d want to stop it in viewDidDisappear.
  2. When starting a timer, it is prudent to make sure you don’t already have a timer running, and if there was one running, stop it before starting another. This is a little bit of defensive programming which we often use with timers, because it is all too easy to accidentally have multiple timers going in parallel. In this case, it is so trivial that this may seem unnecessary, but after you start using timers in more complicated scenarios, you will appreciate timer code that makes it simply impossible to have multiple timers going at the same time.
  3. Note the weak property for the Timer instance. Timers are managed by the runloop on which they run, so the view controller only needs a weak reference. That way, then the timer is invalidated, the run loop will release it, and our reference to the timer will become nil.
  4. In our closure for scheduledTimer, I was conscientious about avoiding strong reference cycle, using [weak self] capture list. This is another defensive programming technique, which ensures that the timer doesn’t keep a strong reference to the view controller. In this simple case, it technically isn’t necessary (viewDidDisappear should be sufficient), but it’s a good habit.

@Pheayuth222
Copy link

Pheayuth222 commented Aug 10, 2023

Good sample, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment