In Swift, a thread refers to a single sequence of execution within a program. By default, code runs on the main thread, which handles tasks sequentially, one after another. This thread is especially important for UI updates.
To prevent the main thread from becoming unresponsive (e.g., when performing long tasks like data fetching), Swift uses concurrency with multiple threads. Apple provides Grand Central Dispatch (GCD), allowing developers to run tasks in parallel on different threads using queues, ensuring smoother app performance without freezing the UI.
There are 3 types of queues that execute with different priorities:
- Main Queue
- Global Queue
- Custom Queue
Main Queue
The Main Queue has the highest priority and is where we should run UI-related operations to avoid crashes. This is used by default, but if we use another queue, we should execute UI-related changes on the Main Queue like this:
DispatchQueue.main.async {
myLabel.text = "Hello"
}
Continue reading
Access to all the content with our plans.
- Junior level content
- Senior level content
- Expert level content
- Extra content
- Question submissions
Be the first to comment