GCD (Grand Central Dispath)
What is GCD?
GCD is the dispatch queue. A queue is actually a block of code that can be executed synchronously or asynchronously, either on the main or on a background thread. Queues are following the FIFO pattern. Next, another important concept is the work item. A work item is literally a block of code that is either written along with the queue creation, or it gets assigned to a queue and it can be used more than once (reused).
GCD is the dispatch queue. A queue is actually a block of code that can be executed synchronously or asynchronously, either on the main or on a background thread. Queues are following the FIFO pattern. Next, another important concept is the work item. A work item is literally a block of code that is either written along with the queue creation, or it gets assigned to a queue and it can be used more than once (reused).
DispatchQueue
- manage tasks you submit and execute them in a FIFO order guaranteeing that the first task submitted is the first one started. Tasks that you submit to a DispatchQueue
are encapsulated by DispatchWorkItem
. You can configure the behavior of a DispatchWorkItem
such as its QoS class or whether to spawn a new detached thread.Queues
- can be either serial or concurrent.
GCD provides three main types of queues:
- Main queue: runs on the main thread and is a serial queue. The main thread must be always remain free so it serves the user interface and user interactions. Any time-consuming or CPU demanding tasks should run on concurrent or background queues.
- Global queues: concurrent queues that are shared by the whole system. There are four such queues with different priorities : high, default, low, and background. The background priority queue is I/O throttled.
- Custom queues: queues that you create which can be serial or concurrent. These actually trickle down into being handled by one of the global queues.
The Quality of Service(QoS) priority are:
- User-interactive: This represents tasks that need to be done immediately in order to provide a nice user experience. Use it for UI updates, event handling and small workloads that require low latency. The total amount of work done in this class during the execution of your app should be small. This should run on the main thread.
- User-initiated: The represents tasks that are initiated from the UI and can be performed asynchronously. It should be used when the user is waiting for immediate results, and for tasks required to continue user interaction. This will get mapped into the high priority global queue.
- Utility: This represents long-running tasks, typically with a user-visible progress indicator. Use it for computations, I/O, networking, continous data feeds and similar tasks. This class is designed to be energy efficient. This will get mapped into the low priority global queue.
- Background: This represents tasks that the user is not directly aware of. Use it for prefetching, maintenance, and other tasks that don’t require user interaction and aren’t time-sensitive. This will get mapped into the background priority global queue.
ref:
https://www.raywenderlich.com/148513/grand-central-dispatch-tutorial-swift-3-part-1
https://www.appcoda.com/grand-central-dispatch/
0 comments: