I want to create a timer, which goes on while i switch to an another ViewController and come back later. My solution is this way:
I start the timer with an button
then i hand over the integer to the next ViewController
I start the timer with the new integer
So my problem results in step three: I want to start the timer in the function viewDidLoad(). But the timer doesn't starts in the next ViewController.
i hope anybody can help me. Tell me, if there is a better way to do the things i want.
Here is my Code:
var timer = Timer()
var eighthours: Int = 8
var activejob: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
identify_activejob()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(Jobs.jobtime), userInfo: nil, repeats: true)
}
//functions
#objc func jobtime() {
eighthours -= 1
}
I can show you one way. But may as suggested, it has native design flaw and use it depends on the accuracy.
let timer = Timer.init(timeInterval: 1, repeats: true) { (timer) in
let string = ISO8601DateFormatter().string(from: Date())
print("running" + string)
}
}
class TimerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
RunLoop.main.add(timer, forMode: RunLoop.Mode.default)
// Do any additional setup after loading the view.
}
}
It will keep running until invalidate.
A couple of thoughts:
The eighthours -= 1 in the timer handler is slightly problematic, because it’s presuming that the Timer will fire, without interruption, at the desired timeInterval. But you should accommodate for interruptions in the Timer (e.g. the UI is blocked momentarily for some reason, the user completely leaves the app and returns, etc.).
We often shift from “decrement some counter with every call of the timer handler” to “figure out at what time we want the timer to expire”. This decouples the “model” (the stop time) from the “view” (the frequency with which the UI is updated). By doing this, if you later decide to update your UI with greater frequency (e.g showing milliseconds rather than seconds, probably using CADisplayLink instead of Timer), it doesn’t change the model driving the app. And it makes your app invulnerable to momentary interruptions that might affect the timer.
If you adopt this pattern, then you can pass around this “stop time”, your model, from view controller to view controller, and each view controller can start and stop its own timer as required by the desired UX for that scene.
So, to start a timer that will stop in 8 seconds:
var stopTime: Date? // set this when you start the timer
func startTimer() {
stopTime = Date().addingTimeInterval(8)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(jobtime(_:)), userInfo: nil, repeats: true)
}
And the timer handler can determine how much time is left with timeIntervalSince to calculate the floating point difference, in seconds, between two dates.
#objc func jobtime(_ timer: Timer) {
let now = Date()
guard let stopTime = stopTime, now < stopTime else {
timer.invalidate()
return
}
let timeRemaining = stopTime.timeIntervalSince(now)
...
}
I also updated jobtime with a timer parameter so that you can see at a glance that it’s a Timer handler.
FYI, your code introduces a strong reference to the view controller that will prevent it from ever being released. This selector-based Timer keeps a strong reference to its target and the run loop keeps a reference to the Timer, so your view controller won’t be released until you invalidate the Timer.
There are a couple of solutions to this:
Start and stop timers as views appear and disappear:
weak var timer: Timer?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(jobtime(_:)), userInfo: nil, repeats: true)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
timer?.invalidate()
}
#objc func jobtime(_ timer: Timer) { ... }
Note, we’re doing this in viewDidAppear and viewDidDisappear (rather than viewDidLoad) to ensure that the starting and stopping of timers is always balanced.
The other pattern is to use block-based Timer, use [weak self] reference to avoid having timer keeping strong reference to the view controller, and then you can invalidate it in the deinit method:
weak var timer: Timer?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
self?.jobtime(timer)
}
}
deinit {
timer?.invalidate()
}
Finally, if you want to update the UI with the greatest possible frequency (e.g. to show milliseconds), you’d probably use a CADisplayLink which is a special timer, perfectly timed for UI updates:
private weak var displayLink: CADisplayLink?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let displayLink = CADisplayLink(target: self, selector: #selector(jobtime(_:)))
displayLink.add(to: .main, forMode: .common)
self.displayLink = displayLink
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
displayLink?.invalidate()
}
#objc func jobtime(_ displayLink: CADisplayLink) { ... }
But the common feature in all of these approaches is that (a) we eliminate strong references from persisting that will interfere with the view controller from getting deallocated when appropriate; and (b) we let every view controller update its UI with whatever frequency it wants.
Related
I am using a timer with repeats: false so it does run one time, in that one time run before reaching the time delay I want to stop executing the timer.
timer?.invalidate()
timer = nil
//is not working
the below code snippet is the sample I tried
var timer : Timer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
timer = Timer()
}
#IBAction func startAction(_ sender: Any) {
timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false, block: { (make) in
print("ack did")
})
}
#IBAction func cancelAction(_ sender: Any) {
timer?.invalidate()
}
so here first I am tapping on start, so the timer starts and wait for specified 10 seconds,
i want that execution to stop when the user tap on cancel. Thanks in advance :)
Yes it's possible, and you've even shown the code to stop the timer. What you've posted will work.
Note that the line
timer = Timer()
In your viewDidLoad() is pointless. Don't create a throw-away timer just to make your variable non-null
I have created a Timer object to repeatedly execute some code for every second in one of the view controllers in my App. My question is will the system automatically invalidate the timer when I pop the view controller off the navigation stack? Somehow my intuition tells me that it doesn't because the timer object itself is not directly tied to the view controller object.
Edit Note: Below is the code for the VC swift file where the timer is created. Please don't bash me for my amateur code. So basically a VC of this type is created and gets pushed onto the navigation stack. Assuming a scenario where the user didn't press the pause button (in which case the timer is invalidated) before going back to the root view by pressing the back button on the navigation bar, will the timer object gets destroyed?
//
// TimerViewController.swift
// SwiftyTimer
//
// Created by Jiaming Zhou on 5/6/20.
// Copyright © 2020 Jiaming Zhou. All rights reserved.
//
import UIKit
class TimerViewController: UIViewController {
#IBOutlet var countDownLabel: UILabel!
#IBOutlet var imageView: UIImageView!
private var timer: Timer?
private var timePassed = -1
private enum status {
case ongoing
case paused
case completed
}
private enum buttonImage {
case cancelButton
case pauseButton
case resumeButton
}
private var state = status.ongoing
var activity: Activity?
override func viewDidLoad() {
super.viewDidLoad()
if let activity = activity {
imageView.image = UIImage(named: activity.name)
view.backgroundColor = UIColor(named: activity.color)
}
//Start a timer that increments every second
updateTimer()
creatTimer()
}
#IBAction func buttonsPressed(_ sender: UIButton) {
switch sender.tag {
case 0:
timePassed = -1
timer?.invalidate()
state = status.ongoing
creatTimer()
updateTimer()
case 1:
if state == status.ongoing {
timer?.invalidate()
timer = nil
state = status.paused
sender.setBackgroundImage(UIImage(named: "\(buttonImage.resumeButton)"), for: .normal)
sender.setTitle("Resume", for: .normal)
} else if state == status.paused {
creatTimer()
state = status.ongoing
sender.setBackgroundImage(UIImage(named: "\(buttonImage.pauseButton)"), for: .normal)
sender.setTitle("Pause", for: .normal)
}
default:
return
}
}
}
//MARK: - Timer
extension TimerViewController {
func creatTimer() {
let timer = Timer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: .common)
self.timer = timer
}
#objc func updateTimer() {
if let activity = activity {
timePassed += 1
if timePassed == activity.duration {
self.timer?.invalidate()
state = status.completed
let alert = UIAlertController(title: "Time's Up!", message: "you have completed your activity", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
}
let currentTime = activity.duration - timePassed
let hours = currentTime / 3600
let minutes = (currentTime / 60) % 60
let seconds = currentTime % 60
var countDown = ""
if hours > 0 {
countDown += "\(hours):"
}
if minutes > 9 {
countDown += "\(minutes):"
} else {
countDown += "0\(minutes):"
}
if seconds > 9 {
countDown += "\(seconds)"
} else {
countDown += "0\(seconds)"
}
countDownLabel.text = countDown
}
}
}
The Timer is not automatically invalidated because when you schedule it, the run loop is keeping a strong reference to it, regardless of whether the view controller has been dismissed or not. There are many ways to solve this, but two contemporary solutions include:
Use completion block Timer:
Use [weak self] pattern, so the timer won’t keep strong reference to self, thus breaking the strong reference cycle.
Have deinit method invalidate the timer when the view controller is deallocated.
For example:
class ViewController: UIViewController {
weak var timer: Timer?
override viewDidLoad() {
super.viewDidLoad()
createTimer()
}
deinit {
timer?.invalidate()
}
func createTimer() {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
self?.handleTimer(timer)
}
}
func handleTimer(_ timer: Timer) { ... }
}
Note, the weak declaration of the timer variable is unrelated to the breaking of the strong reference cycle, but rather serves another purpose, namely to ensure that when the timer is invalidated (should you invalidate it elsewhere), that the timer variable will automatically be set to nil. The key to breaking the strong reference cycle is the [weak self] in the timer’s closure.
The other approach is to use a GCD timer, which will be canceled when you remove your strong reference to it:
Again, use [weak self] pattern for the closure to avoid strong reference cycle.
But unlike Timer, a GCD timer does automatically stop when the DispatchSourceTimer reference is removed. So no deinit method to stop the dispatch timer is needed.
Thus:
class ViewController: UIViewController {
private let timer = DispatchSource.makeTimerSource(queue: .main)
override viewDidLoad() {
super.viewDidLoad()
configureTimer()
}
func configureTimer() {
timer.setEventHandler { [weak self] in
self?.handleTimer()
}
timer.schedule(deadline: .now(), repeating: 1)
timer.resume()
}
func handleTimer() { ... }
}
I’d generally use the Timer approach, but include the GCD DispatchSourceTimer for the sake of completeness.
The system won't automatically invalidate the timer. The view controller has no relation to the timer itself since the timer gets referenced (strongly) by the RunLoop object.
Apple's documentation also explicitly says that the only way to invalidate a timer is to actually call the method:
https://developer.apple.com/documentation/foundation/timer/1415405-invalidate
This method is the only way to remove a timer from an RunLoop object.
The RunLoop object removes its strong reference to the timer, either
just before the invalidate() method returns or at some later point.
If it was configured with target and user info objects, the receiver
removes its strong references to those objects as well.
I have the following code:
class firstVC: UIViewController {
var timer : Timer?
func scheduledTimerWithTimeInterval(){
timer = Timer.scheduledTimer(timeInterval: 60, target: self,
selector: #selector(self.anotherFunc), userInfo: nil, repeats:
true)
}
override func viewDidAppear(_ animated: Bool) {
scheduledTimerWithTimeInterval()
}
}
I'm trying to stop the timer without success:
func stopTimer() {
if timer != nil {
timer?.invalidate()
timer = nil
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
stopTimer()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
stopTimer()
}
I even tried to put the stop function in applicationWillResignActive
And in applicationDidEnterBackground but it didn't stop:
firstVC().stopTimer()
Your help will be appreciated, thank you.
As others have said, you are creating multiple timers without killing the old one before starting a new one. You need to make sure you stop any current timer before starting a new one.
What I do is to make my timers weak. I then use the code `myTimer?.invalidate() before trying to create a new timer:
class firstVC: UIViewController {
weak var timer : Timer?
func scheduledTimerWithTimeInterval(){
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 60, target: self,
selector: #selector(self.anotherFunc), userInfo: nil, repeats:
true)
}
}
By making your timer weak, the only thing that keeps a strong reference to the timer is the run loop. Then, when you invalidate it, it immediately gets released and set to nil. By using optional chaining to call the invalidate method, it doesn't do anything if it's already nil, and stops it and causes it to go nil if it IS running.
Note that this approach only works if you create your timer in one shot using one of the scheduledTimer() factory methods. If you try to create a timer first and then add it to the run loop, you have to use a strong local variable to create it or it gets released as soon as you create it.
The problem is your timer gets instantiated more than once, so the original timer loses its reference.
Add a variable didStartTimer = false.
And then in viewDidAppear do a validation, and then call the timer func.
That should do it.
like this:
class firstVC: UIViewController {
var timer : Timer?
var didStartTimer = false
func scheduledTimerWithTimeInterval(){
timer = Timer.scheduledTimer(timeInterval: 60, target: self,
selector: #selector(self.anotherFunc), userInfo: nil, repeats:
true)
}
override func viewDidAppear(_ animated: Bool) {
if !didStartTimer {
scheduledTimerWithTimeInterval()
didStartTime = true
}
}
After research I found a solution,
The only thing that worked for me is to create functions in AppDelegate file and call them when needed,
Here is the code, the timerSwitch function:
func timerSwitch()
{
if (timerStatus) {
checkStateTimer = Timer.scheduledTimer(
timeInterval: 60,
target:self,
selector: #selector(self.yourFunction),
userInfo: nil, repeats: true)
} else {
checkStateTimer?.invalidate()
}
}
func stopTimer()
{
timerStatus = false
timerSwitch()
}
func startTimer()
{
timerStatus = true
timerSwitch()
}
While 'yourFunction' is what you want to execute when the timer starts,
In my case is sending heartbeat.
Then I called the timerSwitch is the following functions in AppDelegate:
func applicationWillResignActive(_ application: UIApplication) {
stopTimer()
}
func applicationDidEnterBackground(_ application: UIApplication) {
stopTimer()
}
func applicationDidBecomeActive(_ application: UIApplication) {
startTimer()
}
As a debugging step, try putting var timer = Timer() in the global space (e.g. at the top of the file below the import statements) to make sure there's only one Timer object being created and referred to. If you have the Timer declaration within a function, you'll make a new timer each time you call that function, which causes you to lose the reference to the old one, and ultimately not stop it.
Nothing works for me, at last using self did the trick!!!
self.atimer?.invalidate()
self.atimer = Timer()
I'm using nstimer to run code that changes text every 60 seconds.
I was wondering if anyone knew of a way to skip an iteration of the 60 seconds. Say a user doesn't want to read the text currently on display, at the moment they still have to wait 60 seconds.
Is there a way to immediately move on to the next 60 second loop at a button tap?
This is what I have so far:
var array : String[]()
var x = 0
#IBAction func playBtnPressed(sender: UIButton)
{
update()
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
}
func update()
{
if x < array.count {
let item = array[x]
aLbl.text = array.itemTitle
x += 1
}
}
#IBAction func skipBtnPressed(sender: UIButton)
{
}
Thanks in advance for any guidance you can give me! :)
In your skipBtnPressed() you should first invalidate the timer, update data and then set another timer. Like this
#IBAction func skipBtnPressed(sender: UIButton)
{
timer.invalidate()
update()
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
}
I create and start a self-repeating timer in one of my ViewControllers (let's call it VC1) to have some kind of slideshow of images. When transitioning to any other VC the timer of VC1 appears to keep running as its selector method prints stuff every two seconds. Since this interferes with the timer when returning to VC1 from any other VC I have to remove it at some point.
This is what happens in the console: (runImages() is the timer's selector, the number is the image that should be displayed, as you see its weird...)
I thought the timer would stop once I exit VC1 since I do not save it anywhere. Since this is not the case I thought I might remove the timer when leaving VC1. Is there a method that is being called when VC1 is about to be dismissed?
Another approach I had in mind was removing any timers at the beginning of source code of the other VCs. So, when I enter VC2 I want to check for any timers that are running in the project. Is there a way to do that without making the timer a global variable accessible to all VCs?
Code Reference
This is how I create the timer: (outside a method)
var timer: NSTimer!
Then, in a method I set it:
timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "runImages:", userInfo: nil, repeats: true)
runImage() then increases i and calls changeImage() which transitions my imageView's image to the image named like i.
Thanks in advance :)
Update
I made the timer a global variable that is accessible to every VC. The app starts up in VC1, then I transitioned to VC2. There, I inserted this code: if let t = timer {t.invalidate()} and if timer.valid {timer.invalidate()}. Now that made not difference, the timer's selector method keeps printing stuff...
you should keep a reference to the timer in the viewcontroller that is "using" it...
class ViewController: UIViewController {
var timer: NSTimer?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("timerFired"), userInfo: nil, repeats: true)
}
func timerFired() {
// do whatever you want
}
override func viewWillDisappear(animated: Bool) {
if timer != nil {
timer?.invalidate()
timer = nil
}
super.viewWillDisappear(animated)
}
}
Try this
import UIKit
class ViewController: UIViewController {
let timeInterval: NSTimeInterval = 5 // seconds
// The run loop maintains a strong reference already.
weak var timer: NSTimer?
func startTimer() {
// 1. invalidate previous timer if necessary
timer?.invalidate()
// 2. setup a new timer
timer = NSTimer.scheduledTimerWithTimeInterval(
timeInterval,
target: self,
selector: "timerFired",
userInfo: nil,
repeats: true
)
}
func stopTimer() {
timer?.invalidate()
timer = nil
}
// Need to include #objc marker here
// Function must not be private.
#objc internal func timerFired() {
/*
Perform any UI Updates or whatever...
*/
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
startTimer()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
stopTimer()
}
}