【问题标题】:Getting values of children in datasnapshot for firebase on Swift在 Swift 上为 firebase 获取数据快照中子项的值
【发布时间】:2020-07-19 07:26:38
【问题描述】:

本质上,我正在尝试在用户表中使用 programID,并使用该 programID 来获取程序表中的程序详细信息。

我有以下快照:

Snap (-M4HUc52LbUqcweHF0MI) {
    childFirstName = asdf;
    childLastName = aa;
    currentDay = 1;
    currentSession = 1;
    equations = 1;
    numerals = 1;
    problemSolving = 1;
    programmeID = "-M4HUc52LbUqcweHF0MI";
    programmeOwner = YSHHHgkPbhNaWcKsdJok4lXmZEK2;
    quantityRecognition = 1;
    tapToProceed = 1;
}

我正在尝试检索它们的值并将它们存储在下面代码中我自己的 Programs 结构中。我不确定我做错了什么,但我怎样才能获得它们的价值并存储它们?什么是约定?由于某种原因,我找不到任何关于此的文档。

usersRef.child(currentUserID!).child("programmes").observeSingleEvent(of: .value, with: { (snapshot) in
            // Only
            if (snapshot.childrenCount > 0) {
                let programmesRef = Database.database().reference().child("programmes")
                for data in snapshot.children.allObjects as! [DataSnapshot] {
                    let data = data.value as? [String:Any]
                    let currProgID = data!["programmeID"] as! String

                    programmesRef.child(currProgID).observeSingleEvent(of: .value, with: {(snapshot) in
                        print(snapshot)
//                        let individualProgrammes = Programmes(childFirstName: data["childFirstName"] as! String, childLastName: data["childLastName"] as! String, currentDay: data["currentDay"] as! Int, currentSession: data["currentSession"] as! Int, quantityRecognition: data["quantityRecognition"] as! Bool,  equations: data["equations"] as! Bool, problemSolving: data["problemSolving"] as! Bool, numerals: data["numerals"] as! Bool, tapToProceed: data["tapToRegister"] as! Bool)
                    })
programmesRef.child(currProgID).observeSingleEvent(of: .value, with: {(snapshot) in
                        let firstName = snapshot.childSnapshot(forPath: "childFirstName")
                        let lastName = snapshot.childSnapshot(forPath: "childLastName")
                        let day = snapshot.childSnapshot(forPath: "currentDay")
                        let session = snapshot.childSnapshot(forPath: "currentSession")
                        let quantityRecognition = snapshot.childSnapshot(forPath: "quantityRecognition")
                        let equations = snapshot.childSnapshot(forPath: "equations")
                        let problemSolving = snapshot.childSnapshot(forPath: "problemSolving")
                        let numerals = snapshot.childSnapshot(forPath: "numerals")
                        let tapToRegister = snapshot.childSnapshot(forPath: "tapToRegister")

                        let individualProgrammes = Programmes(childFirstName: firstName as! String, childLastName: lastName as! String, currentDay: day as! Int, currentSession: session as! Int, quantityRecognition: quantityRecognition as! Bool,  equations: equations as! Bool, problemSolving: problemSolving as! Bool, numerals: numerals as! Bool, tapToProceed: tapToRegister as! Bool)
                        print(individualProgrammes)

不胜感激。谢谢大家!

【问题讨论】:

    标签: json swift firebase-realtime-database


    【解决方案1】:

    设法解决它!代码如下

    usersRef.child(currentUserID!).child("programmes").observeSingleEvent(of: .value, with: { (snapshot) in
                // Only
                if (snapshot.childrenCount > 0) {
                    for data in snapshot.children.allObjects as! [DataSnapshot] {
                        let data = data.value as? [String:Any]
                        let currProgID = data!["programmeID"] as! String
                        let currProg = Database.database().reference().child("programmes").child(currProgID)
                        currProg.observeSingleEvent(of: .value, with: { snapshot in
                            let data = snapshot.value as? [String:Any]
                            let individualProgrammes = Programmes(childFirstName: data!["childFirstName"] as! String, childLastName: data!["childLastName"] as! String, currentDay: data!["currentDay"] as! Int, currentSession: data!["currentSession"] as! Int, quantityRecognition: data!["quantityRecognition"] as! Bool,  equations: data!["equations"] as! Bool, problemSolving: data!["problemSolving"] as! Bool, numerals: data!["numerals"] as! Bool, tapToProceed: data!["tapToProceed"] as! Bool, dailyRemSessions: data!["dailyRemSessions"] as! Int)
                            // Adding into the programmes array
                            self.programmes.append(individualProgrammes)
                            // Reload the table view after getting the programmes list
                            DispatchQueue.main.async {
                                self.tableView.reloadData()
                            }
                            globalProgrammes = self.programmes
                        })
                    }
                }
            })
    

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 2017-01-30
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 2017-05-16
      • 1970-01-01
      相关资源
      最近更新 更多