Sobes.tech
Junior — Middle

When is it permissible to perform user interface updates outside the main thread?

sobes.tech AI

Answer from AI

In iOS, updating the user interface should only be performed on the main thread, as UIKit is not thread-safe. Exceptions may occur when you use special mechanisms or frameworks that allow asynchronous UI updates, but under the hood, they still switch to the main thread.

For example, if you load data in a background thread, you need to switch to the main thread after receiving the result to update the UI:

DispatchQueue.global().async {
    // Load data
    let data = loadData()
    DispatchQueue.main.async {
        // Update UI
        self.label.text = data
    }
}

Thus, updating the UI outside the main thread is only permissible if you explicitly switch back to the main thread before changing interface elements.

When is it permissible to perform user interface… - sobes.tech