Chapter 13. Blocks and Grand Central Dispatch


This chapter covers

  • Understanding block syntax
  • Handling memory management
  • Using blocks in system frameworks
  • Learning Grand Central Dispatch
  • Performing work asynchronously

Have you ever been to New York City’s Grand Central Terminal? It’s a huge place with trains arriving and departing constantly. Imagine having to manage such an operation by hand—Which train should come in on which track? Which one should change tracks at what time? Which engine should be attached to which train?—and so on and so forth. You’d probably quit your job after 30 minutes and go back to programming!

Being a programmer, however, you might find yourself in a similar situation when you have to write a multithreaded application with different parts of your application being executed at the same time: you must make sure you have enough resources to run another thread; you must make sure that multiple threads can’t manipulate the same data at the same time; you must somehow handle the case of one thread being dependent on other threads, and so on. It’s a headache. And it’s error prone, just like trying to run Grand Central Terminal by yourself.

13.1. The syntax of blocks

13.2. Performing work asynchronously

13.3. Summary