You are using GCD for concurrency, and you would like to know the best way of working with UI-related APIs.
UI-related tasks have to be performed on the main thread, so the
main queue is the only candidate for UI task execution in GCD.
We can use the
get_main_queue function to get the handle to
the main dispatch queue.
There are two ways of dispatching tasks to the main queue. Both are asynchronous, letting your program continue even when the task is not yet executed:
Executes a block object on a dispatch queue.
Executes a C function on a dispatch queue.
cannot be called on the main queue because it
will block the thread indefinitely and cause your application to
deadlock. All tasks submitted to the main queue through GCD must be
Let’s have a look at using the
dispatch_async function. It accepts two
The dispatch queue on which the task has to be executed
The block object to be sent to the dispatch queue for asynchronous execution
Here is an example. This code will display an alert in iOS to the user, using the main queue:
@"GCD is amazing!"