Reputation: 937
I'm trying to execute loadQuestionToView()
after executing loadQuestionsForTests()
using defer
, but I'm unsuccessful... Can you help me with that?
override func viewDidLoad() {
super.viewDidLoad()
defer {
loadQuestionToView()
}
loadQuestionsForTest(testID: testID)
}
func loadQuestionToView() {
questionLabel.text = self.questions[self.questionIterator].question
}
func loadQuestionsForTest(testID: String) {
let docRef = db.collection(Constants.FStore.collectionTests).document(testID).collection("questions")
docRef.getDocuments { (querySnapshot, error) in
if let e = error {
print(e)
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
let question: Question = Question(question: data["question"] as! String, correctAnswer: data["answerCorrect"] as! String, wrongAnswer1: data["answerWrong1"] as! String, wrongAnswer2: data["answerWrong2"] as! String, wrongAnswer3: data["answerWrong3"] as! String)
self.questions.append(question)
//print(self.questions[self.iterator].question)
}
}
}
}
}
Upvotes: 0
Views: 80
Reputation: 363
The best way to do is using closure. If you want learn about closures, here you go: https://docs.swift.org/swift-book/LanguageGuide/Closures.html
override func viewDidLoad() {
super.viewDidLoad()
loadQuestionsForTest(testID: testID) { success in
if success {
questionLabel.text = self.questions[self.questionIterator].questio
} else {
// Whatever you want to do on failure
}
}
}
func loadQuestionsForTest(testID: String, @escaping completion: (Bool) -> Void)) {
let docRef = db.collection(Constants.FStore.collectionTests).document(testID).collection("questions")
docRef.getDocuments { (querySnapshot, error) in
if let e = error {
completion(false)
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
let question: Question = Question(question: data["question"] as! String, correctAnswer: data["answerCorrect"] as! String, wrongAnswer1: data["answerWrong1"] as! String, wrongAnswer2: data["answerWrong2"] as! String, wrongAnswer3: data["answerWrong3"] as! String)
self.questions.append(question)
}
completion(true)
} else {
completion(false)
}
}
}
}
Upvotes: 1
Reputation: 544
override func viewDidLoad() {
super.viewDidLoad()
loadQuestionsForTest(testID: testID, completionHandler: { [weak self] result in
// here I call loadQuestionToView when result is true
if result {
self?.loadQuestionToView()
}
})
}
func loadQuestionToView() {
questionLabel.text = self.questions[self.questionIterator].question
}
func loadQuestionsForTest(testID: String, completionHandler: @escaping (Bool) -> Void) {
let docRef = db.collection(Constants.FStore.collectionTests).document(testID).collection("questions")
docRef.getDocuments { (querySnapshot, error) in
if let e = error {
print(e)
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
let question: Question = Question(question: data["question"] as! String, correctAnswer: data["answerCorrect"] as! String, wrongAnswer1: data["answerWrong1"] as! String, wrongAnswer2: data["answerWrong2"] as! String, wrongAnswer3: data["answerWrong3"] as! String)
self.questions.append(question)
//print(self.questions[self.iterator].question)
}
completionHandler(true)
return
}
}
completionHandler(false)
}
}
Upvotes: 0