user3138007
user3138007

Reputation: 637

Firebase: Access a snapshots children using swift3

I'm trying to get the value of multiple children of my snapshot in order to append my cellDataArray by name and speed. My code is working for name, but not for speed..

ref = FIRDatabase.database().reference().child("BasicInfo")
    let query = ref?.queryOrdered(byChild: "Operator")
    query?.observeSingleEvent(of: .value, with: { (snapshot) in


            for child in snapshot.children.allObjects as! [FIRDataSnapshot] {

                let name = child.key
                let speed = child.childSnapshot(forPath: "Speed")
                self.cellDataArray.append(cellData(mainText: name, Speed: ""))
                self.tableView.reloadData()
        }
    })

This is my Firebase structure: enter image description here

Upvotes: 0

Views: 755

Answers (1)

Nirav D
Nirav D

Reputation: 72460

Try to access the value property of FIRDataSnapshot to get the Speed.

for child in snapshot.children.allObjects as! [FIRDataSnapshot] {
    let name = child.key
    if let dic = child.value as? [String:Any], let speed = dic["Speed"] as? Int
       let operator = dic["Operator"] as? String {  
          print(operator)
          self.cellDataArray.append(cellData(mainText: name, Speed: "\(speed)"))
    }
}
DispatchQueue.main.async {
    self.tableView.reloadData()
}

Upvotes: 3

Related Questions