首页 > 解决方案 > 从 Tableview 中删除值而不希望从 firebase 得到答案

问题描述

当我从 tableView 中删除一个值时,我正在尝试执行代码,但这不起作用..

事实上,我想从 firebase 下载当前值,然后从 firebase 中减去另一个框中给出的值,并且 autoID 包含在我的 tableView 中。所以我必须从 Firebase 下载 2 个信息,然后执行减法并将新值发送到 firebase,然后再擦除我的 tableView 的线。

我目前有这段代码:

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)")

问题出在哪里 ?

谢谢

标签: iosswiftfirebaseuitableview

解决方案


推荐阅读