FLdev

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.

{ 6 comments }

Tampa’s OpenHack Ybor meetup: Tonight at Brass Tap!

by Joey deVilla on September 8, 2015

openhack ybor - new world

OpenHack Ybor’s August gathering at New World Brewery.
Click the photo to see it at full size.

If you’re in the Tampa area tonight and would like to get to know your fellow developers, show off your current passion project, find out what their passion projects are, and enjoy some pizza and beer (or whatever beverage you like) in a friendly, convivial atmosphere, you might want to come to tonight’s OpenHack Ybor meetup!

openhack ybor - new world 2

Another scene from the meetup at New World Brewery.

OpenHack Ybor, held once a month at one of Ybor City’s many beer-dispensing hangouts, is run by local Ruby developer Tony Winn for software developers of all stripes who want to get to know other local developers, see what they’re up to, and enjoy some free pizza. We’ve already had two OpenHack Ybor meetups — the first at the new pub at Coppertail Brewing, and the second at New World Brewery. Tonight’s meetup, which starts at 6:30, takes place at the Brass Tap in Centro Ybor.

centro ybor

Tonight’s meetup location: Brass Tap in Centro Ybor.

If you’d like to attend, RSVP on OpenHack Ybor’s meetup page (there’s no admission, the pizza is free, you’ll have to buy your own beer) so that Tony’s got an idea of how many will be there and can order pizza accordingly. It’s fun, it’s friendly, and it’s one of my go-to geek events. I’ll be there, and I hope to see you there too!

The article also appears in my personal blog, The Adventures of Accordion Guy in the 21st Century.

{ 2 comments }