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

问题描述 投票:0回答:1

我试图从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
                    }




                }
            }
        }
    }

但我没有得到任何数据。

ios swift firebase firebase-realtime-database
1个回答
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)
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.