Chapter 1. Introducing Block Objects

Block objects are packages of code that usually appear in the form of methods in Objective-C. Block objects, together with Grand Central Dispatch (GCD), create a harmonious environment in which you can deliver high-performance multithreaded apps in iOS and Mac OS X. What’s so special about block objects and GCD, you might ask? It’s simple: no more threads! All you have to do is to put your code in block objects and ask GCD to take care of the execution of that code for you.

In this chapter, you will learn the basics of block objects, followed by some more advanced subjects. You will understand everything you need to know about block objects before moving to the Grand Central Dispatch chapter. From my experience, the best way to learn block objects is through examples, so you will see a lot of them in this chapter. Make sure you try the examples for yourself in Xcode to really get the syntax of block objects.

Short Introduction to Block Objects

Block objects in Objective-C are what the programming field calls first-class objects. This means you can build code dynamically, pass a block object to a method as a parameter, and return a block object from a method. All of these things make it easier to choose what you want to do at runtime and change the activity of a program. In particular, block objects can be run in individual threads by GCD. Being Objective-C objects, block objects can be treated like any other object: you can retain them, release ...

Get Concurrent Programming in Mac OS X and iOS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.