Chapter 9
Doing Two Things at Once with Closures

We started the last chapter with the example of a hypothetical household robot that would answer the phone for us. We dealt with the problem of prank callers that never respond to “Hello” by batching together all of our instructions for how to handle the greeting until after the caller responds. That leaves the robot free to do other tasks in the meantime. Now let’s think about how that would work.

Some tasks will require the robot’s limbs, some need its eyesight, and others its voice and hearing. If we’re careful about how we divvy up tasks, the robot can do several things at once: we can prepare dinner while talking on the phone, and our robot should be able to as well.

So let’s imagine we have lists of what each part of our robot’s abilities can be working on: a list of manual tasks, a list of visual tasks, a list of spoken tasks. When it’s time for the robot to continue dealing with the phone call, it can continue working on the manual tasks like cleaning or cooking, uninterrupted by the voice task of handling the call.

Our robot is a multitasking genius. And, if we’re smart, our iPhone can be too.

In this chapter, you’ll learn how Grand Central Dispatch offers the ability to break up work into distinct units—closures—and parcel that work out to whichever CPU core is most able to perform it at that point in time. You’ll also see where the iOS frameworks force you to deal with concurrency, and successfully do so. With this skill in your toolbelt, you’ll be able to keep your user interface fast and responsive as you get long-running tasks out of the way of the UI processing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.187.207