Concurrency in iOS: serial and concurrent queues in Grand Central Dispatch (GCD) with Swift 3

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


UPDATE: I’ve updated this article for Swift 4, learned a few new tricks, and taken advantage of the Swift 4 compiler’s “intelligence.” Please check out the new version as it’s more comprehensive and detailed, and my source code has been highlighted and commented to better help you understand the sometimes confusing concept of parallelism. There’s a brand new Xcode companion project, too.


Today, I’m going to start answering some of the concurrency questions I asked you to ponder in yesterday’s post entitled “Concurrency in iOS — Grand Central Dispatch (GCD) with Swift 3.” Specifically, I’m going to write some code in Swift 3 and Objective-C showing you the difference between serial and concurrent queues. But 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.

Continue reading “Concurrency in iOS: serial and concurrent queues in Grand Central Dispatch (GCD) with Swift 3”

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:

Continue reading “Concurrency in iOS – Grand Central Dispatch (GCD) with Swift 3”

Xcode secrets: save time with context-sensitive help and documentation

How many times have you been looking at Swift or Objective-C code in Xcode and can’t remember what a framework method, argument, constant, etc. means? Did you know that help — full documentation — is just a keystroke away? Did you also know that you can add the same type of pop-up, context-sensitive help to your own code? Here’s how. Let’s say you’re looking at the following NSString class method and can’t remember exactly what the call does, what parameters it takes, and what is its return value:

Highlight the method name, parameter, even the enum, and then press the following key combination on your keyboard:

[command] + [control] + [shift] + [?]

      or, using key symbols:

⌘ ⌃ ⇧ ?

Here’s what you’ll see: a context-sensitive help/documentation popup. Note that I added the red lines to highlight content. The red highlighting is not what Xcode provides (click to enlarge):

Figure 1: Xcode framework context sensitive help and documentation.

So you immediately get information about the method, parameter, enum, even constant’s:

  • Declaration (formal language signature);
  • Description (textual explanation of the entity’s purpose);
  • Parameters (a full list of names and definitions);
  • Returns (the value returned by a method/function, if applicable);
  • Availability (what version of iOS that the entity became available in — and sometimes in what version it was deprecated);
  • Declared In (the framework which contains the entity); and,
  • More (generally, the formal definition of the entity with explanations and other links).

Check out what I get if I click on the “More Type Method Reference” link as shown in Figure 1 (click to enlarge):

Continue reading “Xcode secrets: save time with context-sensitive help and documentation”