O'Reilly logo

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

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

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