I’ve been meaning to get back to regularly featuring articles on programming, and there’s no better time than the present! Global Nerdy will feature a regular category of articles called “Vaya con iOS” (a play on the Spanish blessing, vaya con Dios), which will be about various aspects of iOS development. There’ll also be an Android development category, which you should start seeing in a couple of days.
Let’s get started, shall we?
Getting the hang of GCD
Once upon a time, if you wanted concurrency in your apps, you’d have to use threads. With modern Cocoa systems, which include iOS 4 and later, you have another option: Grand Central Dispatch, or GCD for short. GCD lets you define tasks — functions or blocks — that can be queued for parallel execution on available processor cores, while keeping your code relatively simple and free of all the “yak-shaving” that comes with managing and scheduling threads.
If you’ve been meaning to learn how to program with GCD, Ray Wenderlich’s site has a couple of tutorials that you should find helpful:
- Multithreading and Grand Central Dispatch on iOS for Beginners. If you’re new to GCD and a bit shaky on how Objective-C’s blocks work, read this one first.
- Grand Central Dispatch In-Depth, Part 1. An introduction to the concepts and terminology you’ll need to know, followed by an exercise where you take a nearly-finished project and add GCD functionality to it.
- Grand Central Dispatch In-Depth, Part 2. Now that you’ve got the basics down, some finer details, including semaphores and the perils of too much concurrency.
Avoiding the “staircase of doom”
There are a number of times in Objective-C programming where you end up having one completion block containing another, which contains another, and so on, and in the end, you have to close them all with what Richard Turton calls on his blog, Command Shift, the “staircase of doom”:
“Any time you see this in your code,” writes Turton, “you’re doing it wrong.”
He’s got a couple of articles on avoiding the staircase of doom in certain situations:
- Using dispatch groups to wait for multiple web services: If your app needs to make a number of web services calls, all independent and started by a single method, Turton suggests using a dispatch group to do the job. “A dispatch group monitors work that has been added to it,” he writes, “and it will know when that work is done.”
- Stop nesting animation blocks: “Chaining animations together has always been a little bit awkward. You’d do the first step, then in the completion block, do the second step, then in that completion block, do the third step, and so on, until you close it all off with a staircase of doom.” Or you could use the
animateKeyFramesWithDuration:options:animations:completion:method, and he shows you how to do it.
Talking to the user and other machines
Two of the more recent articles on NSHipster cover communication-related classes:
- AvSpeechSynthesizer: “Introduced in iOS 7,
AVSpeechSynthesizerproduces synthesized speech from a given
AVSpeechUtterance. Each utterance can adjust its rate of speech and pitch, and be configured to use any one of the available
- NSURL / NSURLComponents: “Here [in a URI], in a single, human-parsable string, is every conceivable piece of information necessary to encode the location of any piece of information that has, does, and will ever exist on a computer…A solid grasp of network programming is rooted in an unshakeable familiarity with URL components. As a software developer, this means having a command over the URI functionality in your programming language’s standard library. If a programming language does not have a URI module in its standard library, run, don’t walk, to a real language that does.“