如何在Swift的Firebase中检索嵌套节点内的值

时间:2019-01-31 15:04:35

标签: ios swift firebase firebase-realtime-database

我有一个称为“处方”的节点,“处方”节点包含多个处方。对于每个处方,它都有一些常规数据(处方医师/处方日期/ ...等),还具有多个节点“医学”。每个药物都有数据(药物名称,频率/ ...等)

我的目的是获取每种药物及其一般数据(处方医师/处方日期/ ...等)并将其保存在单独的节点中。

这是我在Firebase中的数据库设计: Database design for "Prescriptions" node

这是我的代码以及要执行的操作:

    refPrescription = Database.database().reference().child("users").child(userID!).child("Prescriptions");

    //observing the data changes
    refPrescription.observe(DataEventType.value, with: { (snapshot) in

        //if the reference have some values
        if snapshot.childrenCount > 0 {
            //clearing the list
            self.prescriptionsList.removeAll()

            //iterating through all the values
            for prescriptions in snapshot.children.allObjects as! [DataSnapshot] {
                //getting values
                let prescribtionObject = prescriptions.value as? [String: AnyObject]
                let physicionName = prescribtionObject?["Prescribed Physician"]
                let prescriptionDate = prescribtionObject?["Prescription Date"]
                let RX = prescribtionObject?["RX"]
                let prescribtionId = prescriptions.key


                //try to get medicines
                self.refRefill = Database.database().reference().child("users").child(self.userID!).child("Prescriptions").child(prescribtionId);

                //observing the data changes
                self.refRefill.observe(DataEventType.value, with: { (snapshot) in

                    //if the reference have some values
                    if snapshot.childrenCount > 0 {
                        //clearing the list
                        self.prescriptionsList.removeAll()

                        //iterating through all the values
                        for medicines in snapshot.children.allObjects as! [DataSnapshot] {
                            //getting values
                            let medicineObject = medicines.value as? [String: AnyObject]
                            let medDose = medicineObject?["Doze"]
                            let medFrequency = medicineObject?["Frequency"]
                            let medName = medicineObject?["Name"]
                            let medQuantity = medicineObject?["Quantity"]
                            let medRefill = medicineObject?["Refill"]
                            let medRefillDate = medicineObject?["nextRefillDate"]


                            self.refRefill = Database.database().reference().child("users").child(self.userID!).child("Refills")
                            let RefillID = self.refRefill.childByAutoId().key

                            let refill1 = ["Prescribed Physician": physicionName as Any, "Refill Date": medRefillDate as Any, "RX": RX as Any, "Prescription ID": prescribtionId as Any, "Dose": medDose as Any, "Frequency": medFrequency as Any, "Name": medName as Any, "Quantity": medQuantity as Any]

                            self.refRefill.child(RefillID!).setValue(refill1)
                        }
                    }
                })

            }
        }
    })

我对获得的输出有些困惑,我想要获得的节点已完美地保存在数据库中。但是,我发现3个复制的节点包含我从第一个循环中获得的一般处方数据(处方医师/处方日期/ ...等)。 Here is my Database after running my code

因此,我该如何修复我的代码,并且不将复制的节点保存到数据库中。

0 个答案:

没有答案