Sobes.tech
Middle

What types of queues exist in Grand Central Dispatch (GCD)?

sobes.tech AI

Answer from AI

GCD uses four main types of queues:

  1. Main Queue:

    • Serial queue.
    • Used for executing tasks on the main thread of the application (UI updates, event handling).
    • Always one per application.
  2. Global Concurrent Queues:

    • Concurrent queues.
    • Provided by the system.
    • Have different levels of Quality of Service (QoS) priority.
    • Can execute multiple tasks simultaneously.
  3. Custom Serial Queues:

    • Serial queues created by the developer.
    • Execute tasks sequentially, one after another.
    • Ideal for managing access to resources (synchronization).
  4. Custom Concurrent Queues:

    • Concurrent queues created by the developer.
    • Can execute multiple tasks simultaneously.
    • Allow better control over task execution compared to global concurrent queues.

Examples of creating queues:

// Main queue
let mainQueue = DispatchQueue.main

// One of the global concurrent queues (high priority)
let globalHighPriorityQueue = DispatchQueue.global(qos: .userInitiated)

// Custom serial queue
let customSerialQueue = DispatchQueue(label: "com.myapp.serialqueue")

// Custom concurrent queue
let customConcurrentQueue = DispatchQueue(label: "com.myapp.concurrentqueue", attributes: .concurrent)

QoS priorities for global queues:

QoS Priority Description
.userInteractive Tasks related to the user interface (response to taps).
.userInitiated Tasks initiated by the user (loading content on request).
.default Default priority, between .userInitiated and .utility.
.utility Long background tasks (downloading files).
.background Low-priority tasks not dependent on the user.
.unspecified System determines the priority.
What types of queues exist in Grand Central Dispatch… - sobes.tech