Swift programming tip: How to execute a block of code after a specified delay

by Joey deVilla on September 30, 2015

burning fuse
swift kick

Sometimes, you want some code to execute after a specified delay. For me, this happens often in user interfaces; there are many cases where I want some notification or other interface element to appear and then disappear after a couple of seconds. I used to use an NSTimer to make it happen, but nowadays, I call on a simple method called delay().

Consider the simple “Magic 8-Ball” app design shown below:

magic 8-ball app

The two functional interface items are the Tap me button and a label that displays a random “yes/no/maybe” answer in response to a button tap. The button should be disabled and the answer should remain onscreen for three seconds, after which the app should revert to its initial state, with the button enabled and the answer label blank.

Here’s the action method that responds to the Touch Up Inside event on the “Tap me” button:

Don’t worry too much about the randomAnswer() method; it simply returns a randomly-selected string from an array of possible answers. The really interesting method is delay(), which takes two parameters:

  • A number of seconds that the system should wait before executing a block of code. In this particular case, we want a 3-second delay.
  • The block of code to be executed after the delay. In our block, we want to blank the label and enable the button.

The block of code that we’re passing to delay() is a closure, which means it will be executed outside the current ViewController object, which in turns means that we’ve got to be explicit when capturing variables in the current scope. We can’t just refer to the button and label as tapMeButton and predictionLabel, but by their fully-qualified names, self.tapMeButton and self.predictionLabel.

Here’s the code for delay():

delay() is just a wrapper for dispatch_after(), one of the functions in Grand Central Dispatch, Apple’s library for running concurrent code on multicore processors on iOS and OS X. dispatch_after() takes three parameters:

  • How long the delay should be before executing the block of code should be,
  • the queue on which the block of code should be run, and
  • the block of code to run.

We could’ve simply used dispatch_after(), but it exposes a lot of complexity that we don’t need to deal with. Matt Neuburg, the author of the O’Reilly book iOS 9 Programming Fundamentals with Swift, found that he was using dispatch_after() so often that he wrote delay() as a wrapper to simplify his code. Which would you rather read — this…

…or this?

Here’s the code for the example “Magic 8-Ball” app, which I’ve put entirely in the view controller for simplicity’s sake:

Resources

zip file iconYou can see this code in action by downloading the zipped project files for the demo project, DelayDemo [220K Xcode 7 / Swift 2 project and associated files, zipped].

If you’d like to learn more about coding for concurrency with Grand Central Dispatch, a good starting place is the tutorial on Ray Wenderlich’s site. It’s a two parter; here’s part 1, and here’s part 2.

I found Matt Neuburg’s delay() method in his answer to this Stack Overflow question.

{ 4 comments… read them below or add one }

1 Jeremy October 15, 2015 at 2:30 pm

Thank you – I have battled to find a method of pausing and it looks like you have the answer.

Many thanks once again

2 MIKE February 5, 2016 at 6:09 pm

Where in Matt’s delay function would you specify the timer’s ‘leeway’ argument?

3 Ariel September 8, 2016 at 5:16 pm

great post!!!

4 Max December 18, 2016 at 9:00 pm

Hi, Im a beginner to coding and I was trying to get a delay after an if statement, but when I write it why do I keep getting an error with the DISPATCH_TIME_NOW? the error message read “Cannot convert value of type ‘Int’ to expected argument type ‘dispatch_time_t’ (aka ‘UInt64’)” then suggests I change to it to “dispatch_time_t(DISPATCH_TIME_NOW)” but all that does is give me more errors. PLEASE HELP!!! (I’m using Xcode 8.1 swift)

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: