Run something before another thing

Hi,
I have this code that I need to run in the exact line order, but I can't figure out how to do it.

Things I have tried:
DispatchQueue Aysnc
completion handler functions

Code:
Code Block
let pickedCat = Int.random(in: 1...4)
//print("picked cat: \(pickedCat)")
if pickedCat == 1 {
getRandomJokes()
randomJoke.text = "Random Joke: \(self.DadJokes)"
//print("Random Joke: \(self.DadJokes)")
}else if pickedCat == 2 {
getAssistantJokes()
randomJoke.text = "Random Joke: \(self.AssistantJokes)"
//print("Random Joke: \(self.AssistantJokes)")
}else if pickedCat == 3 {
getKnockKnockJokes()
randomJoke.text = "Random Joke: \(self.KnockKnockJokes)"
//print("Random Joke: \(self.KnockKnockJokes)")
}else if pickedCat == 4 {
getRandomJokes()
randomJoke.text = "Random Joke: \(self.RandomJokes)"
//print("Random Joke: \(self.RandomJokes)")
}
if randomJoke.text == "Random Joke: " {
randomJoke.text = "Random Joke: Failed to connect to server"
}


Answered by Mcrich23 in 668731022
I figured out that for my case, I can just call it and then wait 0.6 seconds and call it again. To wait blank seconds use (replace 0.6 with the wanted amount):

Code Block
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
// Put your code which should be executed with a delay here
}


Here you go:




Also I have accidentally activated and had to remove a breakpoint before. However, I don't know what they do or how to use them.

Here you go:

Thanks. But as far as I checked, there are no significant difference than the example code I have shown.

Can you show viewDidLoad() and viewWillAppear(_:)?

 I don't know what they do or how to use them.

Better learn it. Xcode Help page is really helpful. Add breakpoints to your code.
Yes here you go:




Also, I read up on breakpoints. However, I have not had the time to figure out what's wrong using breakpoints. Let me know if you want my full project.

Yes here you go:

Thanks for showing your code.

But your viewDidLoad() and viewWillAppear(_:) are modified from what I have shown,
breaking the basic principle:

Do the next thing inside the completion handler.


If you have done any changes breaking this principle, your code would never work as expected.

Or do you still not understand what completion handler means?

 I have not had the time to figure out what's wrong using breakpoints.

I guess you just need time to learn it, or your Xcode is broken. Pleas tell me when you fix the issue and are ready to use breakpoints.


One more, did you yourself write the code you have shown to me? The replies from you makes me feel as if you do not understand any parts of your code.
I have written the majority of my code. I am 13 and I want you to understand that I am still really learning. I will let you know when I have looked through and corrected stuff. That will probably be some time on Thursday.

I want you to understand that I am still really learning.

No problem. Everyone is learning something everyday. One thing I want you to understand is you have no need to pretend you knew everything. You can ask when you find some terms you do not know. You just need to clarify what you do not know.
Hi,
I have determined that for whatever reason. The completion handler isn't being called. Ideas? Would you like to download a copy of my project?

The completion handler isn't being called. Ideas?

That would never happen. This thread is too old and too long, many readers would not visit old and long thread.

Better start a new thread and show the complete code which can reproduce the issue you are facing.
Alright. Thx
Run something before another thing
 
 
Q