Grand Central Dispatch (GCD) is a solution for multi-core programming developed by Apple.
The dispatch queue is divided into the following three types:
1) Run the main queue in the main thread, obtained through Dispatch_get_main_queue.
As can be seen, Dispatch_get_main_queue is also a kind of dispatch_queue_t.
2) Parallel queue Global dispatch queue, obtained by Dispatch_get_global_queue, creates three different priority dispatch queues by the system. Parallel queues are executed in the same order as they join the queue.
3) Serial queue serial queues is typically used to synchronize access sequentially, creating any number of serial queues that are concurrent between each serial queue.
Serial queues are useful when you want tasks to execute in a particular order. A serial queue performs only one task at a time. We can use serial queues instead of locks to protect shared data. Unlike locks, a serial queue guarantees that the task executes in a predictable order.
Serial queues is created through dispatch_queue_create and can be used to increase or decrease the reference count using function Dispatch_retain and dispatch_release.
usage of GCD :
An example of applying GCD:
Another use of GCD is to allow programs to run longer in the background.
When you do not use GCD, when the app is left on the home button, the app only has a maximum of 5 seconds to do some saving or cleanup work. But after using GCD, the app has a maximum of 10 minutes to run in the background for a long time. This time can be used to clean up the local cache, send statistics and other work.
The sample code that lets the program run long in the background is as follows:
IOS-GCD usage