Using DispatchQueue

I am attempting to follow this example of how to create a camera app:

https://www.appcoda.com/avfoundation-swift-guide/

It describes a sequence of 4 steps, and these steps are embodied in four functions:

func prepare(completionHandler: @escaping (Error?) -> Void) {
    func createCaptureSession() { }
    func configureCaptureDevices() throws { }
    func configureDeviceInputs() throws { }
    func configurePhotoOutput() throws { }
    
    DispatchQueue(label: "prepare").async {
        do {
            createCaptureSession()
            try configureCaptureDevices()
            try configureDeviceInputs()
            try configurePhotoOutput()
        }
            
        catch {
            DispatchQueue.main.async {
                completionHandler(error)
            }
            
            return
        }
        
        DispatchQueue.main.async {
            completionHandler(nil)
        }
    }
}

What is confusing me is that it appears these four steps need to be executed sequentially, but in the Dispatch Queue they are executed simultaneously because .async is used. For example farther down that webpage the functions createCaptureSession(), and configureCaptureDevices(), and the others, are defined. In createCaptureSession() the member variables self.frontCamera, and self.rearCamera, are given values which are used in configureCaptureDevices(). So configureCaptureDevices() depends on createCaptureSession() having been already executed, something that it appears cannot be depended upon if both functions are executing simultaneously in separate threads.

What then is the benefit of using DispatchQueue()? How is it assured the above example dependencies are met?

What is the label parameter of the DispatchQueue()'s initializer used for?

Answered by spflanze in 757946022

Never mind. I understand now. All the code inside the DispatchQueue's brackets is the thread, not the individual functions in it.

Accepted Answer

Never mind. I understand now. All the code inside the DispatchQueue's brackets is the thread, not the individual functions in it.

Using DispatchQueue
 
 
Q