Get the data from all children in firebase using swift

前端 未结 2 836
梦毁少年i
梦毁少年i 2020-12-21 19:33

I have a firebase realtime database. It looks like this:

\"Image\"

Here is my code:

ref         


        
相关标签:
2条回答
  • 2020-12-21 20:16

    You'll want to attach the observer one level higher in the JSON, and then loop over the child nodes:

    ref.observeSingleEvent(of: .value) { snapshot in
        for case let child as FIRDataSnapshot in snapshot.children {
            guard let dict = child.value as? [String:Any] else {
                print("Error")
                return
            }
            let latitude = dict["Latitude"] as Any
            let longtitude = dict["Longtitude"] as Any
            print(longtitude)
            print(latitude)
        }
    }
    

    Loop syntax taken from Iterate over snapshot children in Firebase, but also see How do I loop all Firebase children at once in the same loop? and Looping in Firebase

    0 讨论(0)
  • 2020-12-21 20:18

    You need to listen to ref

    ref.observeSingleEvent(of: .value, with: { snapshot in
    
            guard let dict = snapshot.value as? [String:[String:Any]] else {
                print("Error")
                return
            }
            Array(dict.values).forEach {
               let latitude = $0["Latitude"] as? String
               let longtitude = $0["Longtitude"] as? Int
               print(longtitude)
               print(latitude)
            }
    })
    
    0 讨论(0)
提交回复
热议问题