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
}


What do you want in order ?

pickedCat == 1 before 2, before 3 and before 4 ?

If so, the only thing that matters is how you set pickedCat. But you don't show this part of code…
I need the function to run before it sets the text.

I need the function to run before it sets the text.

Which function ? You don't show anything about it. How do you want anyone to guess ?

Please, explain properly what you need and provide relevant information, otherwise, it is impossible to help.
I mean the
Code Block
get_Jokes()

Function. This isn’t running before I set the text and I need it to.
You mean. Sure.

But this func shows nowhere in the code you posted.

Is it that confidential that you cannot show useful code to help you ?
Here is the DadJokes function, they are all the same except for some pass ins:

Code Block
func getDadJokes() {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Dad Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countDadJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countDadJokes))") as? String {
self.DadJokes = Joke
print("Joke: \(self.DadJokes)")
}
}
}
}
}


Thanks for showing.

However, I do not understand how this relates to your initial post.

And what is it you want to have in a specific order.
I want it to:
  1. Pick the category

  2. Get count of the documents on firestore in that category

  3. Randomly pick a document

  4. display that document


  1. Pick the category

    1. Get count of the documents on firestore in that category

    2. Randomly pick a document

    3. display that document

Watching the definition of getDadJokes(), you are using asynchronous calls in #2 and #3.

So, you need to:
  • Do #3 in the completion handler of #2

  • Do #4 in the completion handler of #3

Things I have tried:
DispatchQueue Aysnc
completion handler functions

I guess you used completion handler functions wrongly.
I haven’t really played with completion handlers, could you give me an example?

I haven’t really played with completion handlers, could you give me an example?

Sorry, but you haven't shown enough code to write some example.
Here is the entire function:
Code Block
var countDadJokes = Int()
var countAssistantJokes = Int()
var countKnockKnockJokes = Int()
var countRandomJokes = Int()
func getDadJokes() {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Dad Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countDadJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countDadJokes))") as? String {
self.DadJokes = Joke
print("Joke: \(self.DadJokes)")
}
}
}
}
}
func getAssistantJokes() {
if countAssistantJokes == 0 {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Assistant Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countAssistantJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Assistant Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countAssistantJokes))") as? String {
self.AssistantJokes = Joke
print("Joke: \(self.AssistantJokes)")
}
}
}
}
}
}
func getKnockKnockJokes() {
if countDadJokes == 0 {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Knock Knock Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countKnockKnockJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Knock Knock Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countKnockKnockJokes))") as? String {
self.KnockKnockJokes = Joke
print("Joke: \(self.KnockKnockJokes)")
}
}
}
}
}
}
func getRandomJokes() {
if countDadJokes == 0 {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Random Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countRandomJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countRandomJokes))") as? String {
self.RandomJokes = Joke
print("Joke: \(self.RandomJokes)")
}
}
}
}
}
}
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"
}
}


Here is the entire function:
Thanks for showing the additional codes, but to make things work well with completion handler patter, you may need to modify the caller side.

What is the method header for the lines let pickedCat = Int.random(in: 1...4);... and how are you calling it?

What is the method header for the lines let pickedCat = Int.random(in: 1...4);...and how are you calling it?

I am calling it with the ifs and if elses below it.
Sorry, it may not be clear enough.

For example, the method header for lines if countAssistantJokes == 0 {;... is
Code Block
func getAssistantJokes() {

And the answer for how it is called would be:
Code Block
}else if pickedCat == 2 {
getAssistantJokes() //<-
randomJoke.text = "Random Joke: \(self.AssistantJokes)"
//print("Random Joke: \(self.AssistantJokes)")
}else if pickedCat == 3 {

(Including more context would be welcome.)

Generally, you should better not pick up some fragments of codes, but better show whole method.
Run something before another thing
 
 
Q