I am trying to create an algorithm that creates 12 struct objects(question) and puts them in an array(questions). However, it doesn't seem to work as the objects are created before the data from Firebase has been able to modify them. I was trying to make them asynchronous but nothing that I found online worked. Thanks in advance.
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("NumberOfQuestions").observeSingleEvent(of: .value, with: { snapshot in
while self.questions.count < 12{
var question = questionMGR() //questionMGR is the name of my struct
let questionNumber = String(Int(arc4random_uniform(snapshot.value as! UInt32) + 1))
databaseRef.child("questions").child("questionNumber: " + questionNum).child("title").observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value ?? "")
question.title = (snapshot.value as? String)!
databaseRef.child("questions").child("questionNumber: " + questionNum).child("description").observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value ?? "")
question.desc = (snapshot.value as? String)!
})
})
self.questions.append(question)
}
})