当我从tableView中删除一个值时,我正在尝试执行代码,但这不起作用。
实际上,我想从firebase下载当前值,然后从firebase减去另一个框中提供的值,并且autoID包含在tableView中。因此,我必须从Firebase下载2条信息,然后执行减法并将新值发送到Firebase,然后再删除tableView的ligne。
我目前有此代码:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
if let user = Auth.auth().currentUser{
// user is connect
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
//soustraire les valeurs au total exp
ref.child("Experience").child(userID!).observeSingleEvent(of: .value) {(snapshot) in
if let value = snapshot.value as? [String:String]{
let value = snapshot.value as? NSDictionary
let ldg_day = value?["LDG_DAY"] as? String ?? "0"
let ldg_night = value?["LDG_NIGHT"] as? String ?? "0"
let apch_ifr = value?["APCH_IFR"] as? String ?? "0"
self.oldIntDayLdg = Int(ldg_day) ?? 0
/*self.intLdgNight = Int(ldg_night) ?? 0
self.intApchIfr = Int(apch_ifr) ?? 0*/
self.oldStringDayLdg = ldg_day
/*self.currentStringLdgNight = ldg_night
self.currentStringApchIfr = apch_ifr*/
print("getUserInfo : IntDay : \(self.oldIntDayLdg) & StringDay : \(self.oldStringDayLdg)")
}
}
ref.child("flights").child(userID!).child(datas[indexPath.row].AutoId).observeSingleEvent(of: .value) {(snapshot) in
if let value = snapshot.value as? [String:String]{
let value = snapshot.value as? NSDictionary
let this_flight_ldg_day = value?["LDG_DAY"] as? String ?? "0"
/*let ldg_night = value?["LDG_NIGHT"] as? String ?? "0"
let apch_ifr = value?["APCH_IFR"] as? String ?? "0"*/
self.thisIntDayLdg = Int(this_flight_ldg_day) ?? 0
/*self.intLdgNight = Int(ldg_night) ?? 0
self.intApchIfr = Int(apch_ifr) ?? 0*/
self.thisStringDayLdg = this_flight_ldg_day
/*self.currentStringLdgNight = ldg_night
self.currentStringApchIfr = apch_ifr*/
print("thisFlightInfo : IntDay : \(self.thisIntDayLdg) & StringDay : \(self.thisStringDayLdg)")
}
}
print("Calcul : \(self.newIntDayLdg) = \(self.oldIntDayLdg) + \(self.thisIntDayLdg)")
newIntDayLdg = oldIntDayLdg - thisIntDayLdg
print("int result : \(self.newIntDayLdg)")
newStringDayLdg = String(newIntDayLdg) ?? "00"
print("String result : \(self.newStringDayLdg)")
ref.child("Experience").child(userID!).updateChildValues(["LDG_DAY": self.newStringDayLdg//,
/*"LDG_NIGHT": self.currentStringLdgNight,
"APCH_IFR": self.currentStringApchIfr*/])
ref.child("flights").child(userID!).child(datas[indexPath.row].AutoId).removeValue()
self.tableView.reloadData()
}else{
// si non connecté alors DECONNEXION !!!!
fatalError("⛔️ error ...")
}
datas.remove(at: indexPath.row)
tableView.reloadData()
}
}
但是这个每次给我发送0,而不是好的价值...
let ldg_day = value?["LDG_DAY"] as? String ?? "0"
给我0
和
let this_flight_ldg_day = value?["LDG_DAY"] as? String ?? "0"
也给我0
此外,在我所有的打印文件中,我都检测到代码是首先打印的:
print("Calcul : \(self.newIntDayLdg) = \(self.oldIntDayLdg) + \(self.thisIntDayLdg)")
然后
print("String result : \(self.newStringDayLdg)")
并且仅在两次打印之后:
print("getUserInfo : IntDay : \(self.oldIntDayLdg) & StringDay : \(self.oldStringDayLdg)")
&
print("thisFlightInfo : IntDay : \(self.thisIntDayLdg) & StringDay : \(self.thisStringDayLdg)")
问题出在哪里?
谢谢