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:
These facilitate the execution of one or more block objects.
These allow you to invoke a method in another, currently existing object.
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
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
An operation object can have dependencies on other operation ...