Open In App

Difference between Repeating and Non-Repeating timers in Swift

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Swift uses timers to create recurring activities and delay the start of some processes. It is a class that was once referred to as NSTimer. This class gives a flexible method for planning tasks that will happen in the future, either once or repeatedly.

You frequently encounter situations where you must repeatedly do a specific activity within a specific time frame or situations where you must complete specific actions after a specified period of time. These are the best circumstances for utilizing Timers in your codebase. Some typical use cases for timers include:

  • When you have to call a specific method automatically every 5 seconds.
  • On app termination, you want to send a local notification.
  • Send new information after X times to sync your cart details with the backend.

To achieve the repeating and scheduling behavior in the aforementioned instances, timers can be used.

Repeating Timer

A repeating timer is a timer that fires and reschedules itself in the same loop. Or we can say that a repeating timer always schedules itself according to the scheduled fire timing. A repeating counter can be created and launched using the following syntax.

Syntax:

let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)  

Here, the used parameters are-

  • timeInterval: The timer interval is specified in seconds, and the value’s type is Double.
  • target: It designates a class instance on which the selector function should be invoked.
  • selector: It uses #selector to provide the function to call when the timer goes off (…)
  • userInfo: It designates either nil or a dictionary with data supplied to the selection.
  • repeats: It indicates if this timer repeats or doesn’t repeat.

Example:

Swift




// Swift program to create a repeating timer
  
let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self
                                 selector: #selector(fire), 
                                 userInfo: nil, repeats: true)  
@objc func fire()   
{  
    print("Fire the repeating timer")
}


Here, a timer is made using the Timer.scheduledTimer(…) class method. The return value of this procedure is assigned to the constant timer. There is now a timer reference in this constant that will be used in the future. The scheduledTimer() arguments set the timer interval to 5 seconds. It makes use of the target-action mechanism, a minimal amount of nil userInfo, and repeats set to true. Additionally, we created a function called fire (). Every time the timer is activated, which happens roughly every second, this method is called. When the timer fires, the self’s function fire() must be called, which is indicated by setting the target to self and the selector to #selector(fire).

Nonrepeating Timer

A non-repeating timer is a timer that fires only once and then invalidates itself automatically to prevent itself from firing again. To create a non-repeating timer simply change the repeats option to false. Only one firing will occur before the timer invalidates itself.

Syntax:

let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: false)  

Here, the used parameters are-

  • timeInterval: The timer interval is specified in seconds, and the value’s type is Double.
  • target: It designates a class instance on which the selector function should be invoked.
  • selector: It uses #selector to provide the function to call when the timer goes off (…)
  • userInfo: It designates either nil or a dictionary with data supplied to the selection.
  • repeats: It indicates if this timer repeats or doesn’t repeat.

Example:

Swift




// Swift program to create a non-repeating timer
  
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self
                                 selector: #selector(fire), 
                                 userInfo: nil, repeats: false)  
@objc func fire()   
{  
    print("Fire the non repeating timer")  
}


Difference between Repeating and Non-Repeating timers

Following are the difference between repeating and non-repeating timers:

Non-Repeating Timers  

Repeating Timers

Non-repeating timers only fire once before immediately invalidating themselves to stop further firing.  A repeating timer starts and then restarts itself on the same run loop. 
Simply setting the repeats parameter to false will result in a non-repeating timer. The timer will only start once and then instantly stop working. The scheduled firing time, not the actual firing time, is always used to schedule a repeating timer. Instead of using the actual firing time to plan itself, a repeating timer always uses the scheduled firing time.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads