DispatchQueue Help

I want to execute two functions - lets say functionA and functionB.

I need functionA to finish executing before funcB executes because functionB uses data from functionA.

I thought I would use DispatchQueue.main.async to accomplish this...


DispatchQueue.main.async(execute: {
    self.functionA()
    self.functionB()
})


When I use it, functionA gets executed, but functionB does not get executed.


I'd appreciate any help.

Thanks

This isn't the way to do it. For a start, what you've done is more or less exactly the same as executing the code in the closure on the main thread, simply:


    self.functionA() 
    self.functionB()


Dispatching them asynchronously just delays the start of functionA slightly, and nothing else.


Presumably, functionA actually has some asynchonous effect of its own. If it didn't it would do all its works synchronously, which would block the main thread, and you'd be raising that issue instead. So, currently, you're starting functionB after functionA has begun its work, but not finished. That means functionB has no data to work with, and so does nothing. (There's some guesswork in all of that, so the details might not be right).


Therefore, I'm guessing, what you really want is to start functionB after functionA's incomplete work has actually finished. For that, you need an asynchronous pattern, where (for example) functionA takes a completion handler closure, which it executes it when its work is done:


    self.functionA(completionHandler: {
        self.functionB() })


or, a bit more attractively, with trailing closure syntax:


    self.functionA {
        self.functionB() }


Again, I'm guessing at things you didn't give any details on, but an asynchronous pattern sounds like the sort of thing you need.

Thank you for your response.

You are correct in what I am trying to do.

I tried both ways that you suggested, but received the same error for both... "Argument passed to call that takes no arguments"

Is there something I need to add to functionA to accept the argument?


Thanks


P.S. Something unrelated, but may be related to the solution... functionB displays a UIAlert message.

Normal, functionA has no argument.


Should write:


func functionA(completionHandler: () -> Void) {
    print("Function A")
    completionHandler()     // if you need to execute the completion handler here
}

func functionB() {
    print("Function B")
}

functionA(completionHandler: {
    functionB() })

functionA {
       functionB() }


To get

Function A

Function B

Function A

Function B

Thank you for your reply.

I must be doing something wrong.

I entered it exactly as you described and mine prints "Function A" but never prints "Function B" so functionB is still not being executed.


Updated:


I see a part I left out. Now it does enter functionB but it does not show the UIAlert window that functionB is suppose to show.

Maybe a UIAlert can not be executed in a function that is within a closure?

UIAlert must be executed from the main thread


Enclose all the alert part in

DispatchQueue.main.async {

}
DispatchQueue Help
 
 
Q