当我尝试从我的数据库中获取isBusy属性的值时,我遇到了问题。一个使用过的断点,它甚至不会转到我为isBusy赋值的指令,它指定空字符串并转到else条件。
if docChild == 0 {
let selectedDoctor = Doctors.doc1
var isBusy = String()
databaseHandle = reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.isBusy).observe(.childAdded, with: { (snapshot) in
isBusy = snapshot.value as! String
})
if isBusy == "false" {
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.isBusy).setValue("true")
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.firstname).setValue(firstNameLabel.text)
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.lastname).setValue(lastNameLabel.text)
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.pesel).setValue(peselLabel.text)
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.telephone).setValue(phoneLabel.text)
} else {
createAlert(title: "Przepraszamy - termin jest zajęty", message: "Proszę wybrać inny termin")
}
答案 0 :(得分:1)
答案很简单。 Firebase数据库请求是异步的,因此在您到达if isBusy == "false"
语句时,不会从数据库中获取值。相应地更改您的代码:
if docChild == 0 {
let selectedDoctor = Doctors.doc1
var isBusy = String()
databaseHandle = reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.isBusy).observe(.childAdded, with: { (snapshot) in
isBusy = snapshot.value as! String
if isBusy == "false" {
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.isBusy).setValue("true")
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.firstname).setValue(firstNameLabel.text)
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.lastname).setValue(lastNameLabel.text)
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.pesel).setValue(peselLabel.text)
reference.child(selectedDoctor).child(dateChild).child(timeChild).child(ReservationFields.telephone).setValue(phoneLabel.text)
} else {
createAlert(title: "Przepraszamy - termin jest zajęty", message: "Proszę wybrać inny termin")
}
})
正如您所看到的,我只是将您的代码移动到处理程序中,因此在>>获取您的值后执行。