7.10. Constructing Your Own Dispatch Queues with GCD

Problem

You want to create your own uniquely named dispatch queues.

Solution

Use the dispatch_queue_create function.

Discussion

With GCD, you can create your own serial dispatch queues (see Recipe 7.0 to read about serial queues). Serial dispatch queues run their tasks in a first-in-first-out (FIFO) fashion. The asynchronous tasks on serial queues will not be performed on the main thread, however, making serial queues highly desirable for concurrent FIFO tasks.

All synchronous tasks submitted to a serial queue will be executed on the current thread being used by the code that is submitting the task, whenever possible. But asynchronous tasks submitted to a serial queue will always be executed on a thread other than the main thread.

We’ll use the dispatch_queue_create function to create serial queues. The first parameter in this function is a C string (char *) that will uniquely identify that serial queue in the system. The reason I am emphasizing system is because this identifier is a system-wide identifier, meaning that if your app creates a new serial queue with the identifier of serialQueue1 and somebody else’s app does the same, the results of creating a new serial queue with the same name are undefined by GCD. Because of this, Apple strongly recommends that you use a reverse DNS format for identifiers. Reverse DNS identifiers are usually constructed in this way: com.COMPANY. PRODUCT. IDENTIFIER. For instance, I could create two serial ...

Get iOS 7 Programming Cookbook 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.