Chapter 7. Operations, Threads, and Timers

7.0. Introduction

This chapter shows how to run multiple tasks on iOS. The basic operating system mechanism for spinning off tasks is threading, but most iOS programmers prefer higher-level abstractions such as operations. Timers are another useful feature for handling separate tasks.

Operations can be configured to run a block of code synchronously or asynchronously. You can manage operations manually or place them on operation queues, which facilitate concurrency so that you do not need to think about the underlying thread management. In this chapter, you will see how to use operations and operation queues, as well as basic threads and timers, to synchronously and asynchronously execute tasks in applications.

Cocoa provides three different types of operations:

Block operations

These facilitate the execution of one or more block objects.

Invocation operations

These allow you to invoke a method in another, currently existing object.

Plain operations

These are plain operation classes that need to be subclassed. The code to be executed will be written inside the main method of the operation object.

Operations, as mentioned before, can be managed with operation queues, which have the data type NSOperationQueue. After instantiating any of the aforementioned operation types (block, invocation, or plain operation), you can add them to an operation queue and have the queue manage the operation.

An operation object can have dependencies on other operation ...

Get iOS 4 Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.