O'Reilly logo

iOS 4 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required