-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
The DispatchQueue.main.async(execute:) function will execute code on the application’s main queue. We generally use this function when we want to update the UI or perform tasks that require changes to the UI.
The main queue is automatically created for the main thread when the application starts. This main queue is a serial queue; therefore, items in this queue are executed one at a time, in the order in which they were submitted. We will generally want to avoid using this queue unless we have a need to update the user interface from a background thread so that we do not block the main thread, which may make the user interface unresponsive.
The following example shows how we would use this function. For this code, it is assumed that an extension was used to add a method to the UIImage type that will resize the image:
let squeue = DispatchQueue(label: "squeue.hoffman.jon")
squeue.async {
let resizedImage = image...