如何从firebase数据库中的多个子节点检索数据

时间:2017-12-24 21:25:05

标签: ios swift firebase firebase-realtime-database

我试图从firebase检索数据。我可以从一个孩子中检索数据。但是如何从许多孩子那里检索数据?

enter image description here

我试过这种方式

    func fatchSchdual(){
        ref.child("Products").child(productdetailes[myIndex].userId!).child("Schedule").child("0").observeSingleEvent(of: .value) { (snapshot) in

            if snapshot.childrenCount > 0{

                for Schdu in snapshot.children.allObjects as! [DataSnapshot]{
                    let schdual = Schdu.value as? [String: AnyObject]
                    let startTime = schdual?["start"]
                    let endTime = schdual?["end"]

                    if let StartTime = startTime{
                        self.publishTime.text = StartTime as? String
                    }




                }
            }
        }
    }

但我没有得到任何数据。

1 个答案:

答案 0 :(得分:1)

func fatchFacilities(){
    ref.child("Products").child(productdetailes[myIndex].userId!).child("Facility").observeSingleEvent(of: .value) { snapshot in

        for child in snapshot.children {
            if let value = (child as! DataSnapshot).value as? [String : Any] {
                let name = value["name"] as? String
                let unit = value["unit"] as? String
                let facility = Facility(name: name, unit: unit)
                facilities.append(facility)
            }
        }
    }
}