Im trying to query and update data to firebase. But I cannot get the data. It seems the program doesn't run into ref.observeSingleEvent what should I do to fix this?
func getId(path:String, value:String?, completion: @escaping (String?) -> Void) {
let ref = Database.database().reference(withPath: path).queryOrdered(byChild: "name").queryEqual(toValue: value)
ref.observeSingleEvent(of: .childAdded, with: { snapshot in
print("running in getId")
completion(snapshot.key)
}) { (error) in
print(error.localizedDescription)
}
}
@objc func onTapConfirmed(){
self.getId(path: "patients", value: self.kardex?.patient?.name) { snapshotKey in
BookingViewController.kardexDict["patient"] = snapshotKey as AnyObject?
print("1:get patient name")
}
self.getId(path: "nurses", value: BookingTreatmentListController.nurseName) { snapshotKey in
BookingViewController.kardexDict["nurse"] = snapshotKey as AnyObject?
print("2:get nurse name")
}
Database.database().reference().child("kardexes").child(BookingViewController.newKardex.id).updateChildValues(BookingViewController.kardexDict)
print("save kardexDict")
}
I expect to get the following result
"1:get patient name" -> "running in getId" -> "2:get nurse name" -> "running in getId" -> "save kardexDict"
But I got
"1:get patient name" -> "2:get nurse name" -> "save kardexDict"
and the data in kardexDict is not correct since the data is not obtained from function getId() What can I do to force the program follow my order I expected.