Concurrency in iOS – Grand Central Dispatch (GCD) with Swift 3

[Download the full Swift Xcode project from GitHub.]
[Download the full Objective-C Xcode project from GitHub.]

I use concurrency in most of my iOS apps, generally to keep the user interface responsive. For those of you new to iOS and/or new to computer science, “Concurrency is the notion of multiple things happening at the same time.” (We can discuss the old “What’s the difference between concurrency and parallelism?” question later.) I’m constantly being asked questions about how to implement concurrency in iOS and, most recently, I’ve received many questions about how to implement it in Swift 3. Here’s a video showing this post’s concurrent Swift code running — and notice that a UIProgressView is updating while images are downloading in the background; notice also that I’m continually pressing a button that increments an instance variable during background processing:

(Make sure you stay with me to complete this discussion of iOS concurrency in the next post. I’ll write some code in Swift 3 and Objective-C showing you the difference between serial and concurrent queues. Before coding we’ll talk about concurrency in general, the terminology used in discussing concurrency (threads, process, and tasks), the differences between the terms “concurrent” and “parallel,” the differences between serial and concurrent queues, the differences between synchronous and asynchronous methods/functions, and finally we’ll wrap up with some more definitions you need to know about.)

The questions about implementing GCD in Swift 3 have come from beginners to intermediate- to even advanced-level developers. For years, the most widely used iOS construct for concurrency — “starting tasks asynchronously” — has been Grand Central Dispatch (GCD). Most iOS old-timers like me have gotten used to GCD’s Objective-C language version of the “dispatch_async” function:


For example, many iOS developers have used “dispatch_async” to start the download of images “in the background.” While the images download, their apps’ user interfaces stay responsive. Once each download completes, they jump onto the “main thread” to display the image to the user. Notice I’ve placed some terms in quotes. We’ll define those terms later. My challenge to you is to research any terms with which you’re not familiar. For those of you in a hurry to find out how I now use GCD in Swift 3…

I found some URLs to very beautiful and high resolution images at NASA’s website. Then I wrote a sample project in which I download a fixed number of these images in the background and, as each image finishes downloading, I display it to the user in a UIImageView and update a UIProgressView to show the overall progress of downloading and displaying all images. Here’s a snippet of the most important method in my project showing off GCD in Swift 3:

Remember that a lot of iOS apps were written in Objective-C and are still being actively maintained and updated. Swift has changed so many times that developers have had to rewrite the same code multiple times. Many Apple iOS frameworks were/are written in Objective-C. While Swift is a promising new language, you should still learn — at least be knowledgeable in — Objective-C. One great advantage with using Objective-C: As new Xcode versions keep coming out, you rarely have to rewrite much code. Here’s how you would write code to achieve the same type of GCD functionality in Objective-C:

#ad
   

We’re not finished discussing concurrency. See the next post in this series. I want you to do some thinking and research to find out more about “the notion of multiple things happening at the same time” in code. Specifically, please define these terms and/or concepts:

[Download the full Swift Xcode project from GitHub.]
[Download the full Objective-C Xcode project from GitHub.]


Author: Andrew Jaffee

Avid and well-published author, software engineer, designer, and developer, now specializing in iOS mobile app development in Objective-C and Swift, but with a strong background in C#, C++, .NET, JavaScript, HTML, CSS, jQuery, SQL Server, MySQL, Oracle, Agile, Test Driven Development, Git, Continuous Integration, Responsive Web Design, blah, blah, blah ... Did I miss any fad-based catch phrases? My brain avatar was kindly provided by https://icons8.com under a Creative Commons Attribution-NoDerivs 3.0 Unported license.