[GCD] Basics of parallel programming in Swift

What is this article?

I investigated parallel programming using GCD.

environment

macOS 10.15.7 Xcode 12.1 Swift 5.3

the term

thread

Every iOS application has a main thread. The main thread displays the UI and waits for events that occur. Complicated calculations in the main thread can slow down the process and freeze the app. Multithreading is useful in such cases. It is a mechanism that moves a task with a high load to a background thread and returns to the main thread when the task is completed.

queue

A set of code blocks in parallel or in series waiting to be executed by a thread. With Swift, you only need to ** manage queues ** and you don't have to be thread-aware at all. The system is responsible for providing and allocating the ** threads needed to execute the code in the queue. ** **

Grand Central Dispatch: GCD API responsible for managing queues.

Role of GCD

--Create a queue --Deposit the code block in the queue

Main queue

The most important queue of all. ** Any block of code that affects the UI must be executed in the main queue **.

Background queue

Used to queue tasks other than long-lived UI tasks. Often runs in parallel with the main UI queue.

Background queue priority level

User interactive

.userInteractive User interactive tasks are the highest priority tasks on the system. Use this level for tasks and queues that interact with users and actively update the app's user interface. For example, use it for animations or classes that interactively track events.

User Initiative

.userInitiated User initiative tasks are second only to user interactive tasks in terms of system priority. Assign this class to tasks that give immediate results while the user is doing something, or tasks that prevent the user from using the app. For example, you can use this Quality-of-Service level to load the content of an email that you want users to see.

utility

.utility Utility tasks have a lower priority than user-initiated tasks and user interaction levels, but higher priority than background tasks. Assign this quality of service class to tasks that do not prevent users from continuing to use your app. For example, you can assign this class to long-term tasks where users do not actively follow their progress.

Background

.background Background tasks are the lowest priority of all tasks. Assign this level to the task or dispatch queue that your app uses to perform work while running in the background. For example, maintenance tasks or cleanup.

standard

.default Priority is a level between user-initiated tasks and utilities.

Add code to the queue

Add synchronously

If the task item is executed synchronously by the sync method, the program waits for the execution to finish before the method is called.

python


DispatchQueue.main.sync {
	// Code to be executed
}

This code can be understood as "dispatching a synchronous queue to the main thread".

Add asynchronously

When a task item is executed asynchronously by the async method, the method is called immediately.

python


DispatchQueue.global().async {
	// Code to be executed
}

This code can be understood as "dispatching an asynchronous queue to a background thread".

Recommended Posts

[GCD] Basics of parallel programming in Swift
[GCD] Basics of DispatchQueue class
Basics of sending Gmail in Ruby
Find an approximation of cosx in Swift
The story of learning Java in the first programming
java programming basics
SKStoreReviewController implementation memo in Swift UI of iOS14
Basics of Ruby
Basics of threads and Callable in Java [Beginner]
Order of modifiers used in Swift (see SwiftLint)
How to execute tasks in parallel in Swift in Swift Package
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Find the approximate value of log (1 + x) in Swift
Compare objects in Swift
Constraint programming in Java
[Swift] Asynchronous processing "GCD"
Java programming basics practice-array
Division becomes 0 in Swift
Basics of try-with-resources statement
Output in multiples of 3
Parallel execution in Java
Multidimensional array in Swift
[Swift 5] Recognize ON/OFF of Switch in custom cell for each cell
List of devices that can be previewed in Swift UI
How to change BackgroundColor etc. of NavigationBar in Swift UI