Получение значений детей в datasnapshot для firebase на Swift - PullRequest
0 голосов
/ 07 апреля 2020

По сути, я пытаюсь использовать programmeID в пользовательской таблице и использую этот programmeID для получения сведений о программе в таблице программ.

enter image description here

У меня есть снимок со следующим:

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

, и я пытаюсь получить их значения и сохранить их в моей собственной структуре программ в коде ниже. Я не уверен, что я делаю неправильно, но как я могу получить их ценность и сохранить их? Что такое конвенция? По какой-то причине я не могу найти документацию по этому вопросу.

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)

Буду признателен за помощь. Спасибо, ребята!

1 Ответ

0 голосов
/ 07 апреля 2020

удалось решить! Код ниже

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
                    })
                }
            }
        })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...