Sobes.tech
Middle

What types of queues are used in main and global?

sobes.tech AI

Answer from AI

Main queue - is a serial queue that runs on the main thread of the application. It is used for updating the user interface and handling UI events. All tasks dispatched to the main queue are executed strictly sequentially.

Global queues - are concurrent queues provided by the system. They are used for executing background tasks unrelated to the UI, which helps avoid blocking the main thread. There are several global queues with different Quality of Service (QoS) levels.

Here is a table of the main types of global queues by QoS priority:

QoS Priority Description Example usage
userInteractive Highest priority. Used for tasks that require immediate execution for UI interactivity. Animations, touch event handling.
userInitiated High priority. Used for tasks directly initiated by the user that require quick results. Displaying search results, loading content for display.
default Default priority. Between userInitiated and utility. Tasks for which no priority is explicitly specified.
utility Medium priority. Used for long-running tasks executing in the background with progress. File downloads, data processing.
background Low priority. Used for tasks running in the background that do not require user attention. Data synchronization, backup.
unspecified Priority not specified. The system determines it based on other factors. Rarely used.

Example of dispatching a task to the main and a global queue:

DispatchQueue.main.async {
    // Code to update UI
}

DispatchQueue.global(qos: .userInitiated).async {
    // Code to perform a high-priority background task
    // After completion, it might be necessary to return to the main queue to update UI
    DispatchQueue.main.async {
        // UI update after background task completion
    }
}
What types of queues are used in main and global? — iOS - sobes.tech