I am trying to write a function to get data from firebase and update a global variable (branch), I understand that the background thread responsible for that so the main thread can continue even-though the background thread haven't finished yet, but I don't want that. How can I force the main thread to wait until the global variable is updated?
Here is the function:
func getID(email: String){
databaseRef = Database.database().reference()
databaseRef.root.child("Branches").observe(.value, with: { (snapshot) in
if ((snapshot.children.allObjects as? [DataSnapshot]) != nil) {
let child = snapshot.value as! [String: [String : String]]
for (key, data) in child{
for (k, d) in data{
if k == "Email" && d == email {
self.branch = key
}
}
}
}
})
}