I currently have a button that when pressed checks if a function returns a true or false parameter. The function in question checks a code in a database and returns true if it is there, and false if it is not. However, the function returns false by default as it takes a second for the database to return a response. Is there any way for me to get the function to wait for the response to come in before returning a response to the if statement in question? That way when the button is pressed and the if statement runs which checks if the function result is true or false, it gets an accurate response? Enclosed is the if statement itself, as well as the function.
if ps.getNEWStatus(name: partnerCodeField.text!) == true {
partnerCode = partnerCodeField.text!
ps.getStatus()
} else if ps.getNEWStatus(name: partnerCodeField.text!) == false {
holdText.text = "Partner code is invalid."
}
Function
func getNEWStatus(name: String) -> Bool {
db.collection("users").document(name)
.addSnapshotListener { documentSnapshot, error in
guard let document = documentSnapshot else {
// print("Error fetching partner status: \(error!)")
return
}
guard let data = document.data() else {
// print("No partner with this code exists.")
isPartnerValid = false
return
}
if let isOnline = data["isOnline"] as? Int {
isPartnerValid = true
}
}
if isPartnerValid == true {
return true
} else if isPartnerValid == false {
return false
}
return false
}