How to add a delay to code execution in Swift

In this tutorial, we will learn how to add a delay to code execution in Swift language.

While developing any application in Swift, we mostly need our code to delay the execution process. We can achieve this functionality using various delay methods and blocks provided by Swift. Some of them are:

  • asyncAfter()
  • perform()
  • scheduledTimer()

Using asyncAfter block in Swift

It is a built-in method of Grand central dispatch(GCD) API which helps us to add delay to our code execution in swift. The code that is to be delayed must be placed inside the asyncAfter() block. As a result, the code only executes after the specified time.

Syntax:

DispatchQueue.main.asyncAfter(deadline: .now() + seconds)
{
//we can place our code here
}

In place of seconds, we need to specify the time in seconds after which the code is to be executed.

Program to implement the asyncAfter block

print("Before delay")

DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    print("After delay")
}

Output:

Before delay
After delay

In the above code, we placed a print statement in the block as an example which executes after 2 seconds. Instead, we can also call the functions in our asyncAfter block that executes after the specified time.

Using perform method

The perform method adds delays to the execution of our code, especially to a function. It calls the specified function only after the specified time delay.

Syntax:

perform(#selector(function_name), with: argument_type, afterDelay: seconds)
@objc func function_name() {

         //we can place our code here

}

It has three parameters they are selector, with, afterDelay. In the selector, function_name indicates the name of the function on which the delay is added. The with parameter is the type of argument we pass to the specified function. The afterDelay indicates the amount of delay.

Program to implement the perform method

let delay = 3.0
perform(#selector(myFunc), with: nil, afterDelay: delay)

@objc func myFunc() {
    print("This messaged delayed for 3 seconds")
}

Output:

This messaged delayed for 3 seconds

In the above code, we passed our myFunc method to the selector. Here our method takes no argument hence we specified with parameter as nil. As a result, the myFunc method gets called after specified 3 seconds.

Using scheduledTimer block

We can also add delay to our code execution using the scheduledTimer block provided by the Timer class. Similar to the above methods, here also we specify the amount of delay in seconds.

Syntax:

let var = Timer.scheduledTimer(withTimeInterval: seconds, repeats: boolean_value)
{ (var) in

    //we can place our code here

}

In this block, withTimeInterval specifies the delay time and repeats specify whether the timer is repeating(when true) or fires only once(when it is false).

Program to implement the scheduledTimer block

let varTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) 
{ (varTimer) in
    print("This message delayed for 2 seconds")
}

Output:

This messaged delayed for 2 seconds

In the above code, we specified the time interval as 2 seconds and repeats value as false. As a result, the code fires only once after the delay of 2 seconds.

That’s it! Hope you understood how to add delay to code execution in Swift.

Also read,

Leave a Reply

Your email address will not be published.