Faizul Karim
Faizul Karim

Reputation: 353

How can I retrieve data from many child in firebase database

I was trying to retrieve data from firebase. I can retrieve data from a single child. But how can I retrieve data from many child?

enter image description here

I tried this way

    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
                    }




                }
            }
        }
    }

but I didn't get any data.

Upvotes: 0

Views: 78

Answers (1)

yakovlevvl
yakovlevvl

Reputation: 524

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)
            }
        }
    }
}

Upvotes: 1

Related Questions