iosswiftfirebasefirebase-realtime-databaseswift4

How to get a specific value in firebase-realtime-database in swift 4.0


I am having trouble trying to get a specific value in firebase-realtime-database in swift 4.0

Here's the value I want to get that is stored in a varible

let value = 12345678

Here's what my firebase database looks like

enter image description here

And I want find the trackId that has the same value as my variable and then get that trackId

I have spent a whole week on this problem I can't seem to solve it any help would be really appreciated.

heres my code so far

let questionPostsRef = self.ref.child("questionPosts")
let query = questionPostsRef.queryOrdered(byChild: "trackId").queryEqual(toValue: "12345678")
query.observeSingleEvent(of: .value, with: { snapshot in
    for child in snapshot.children {
        let childSnap = child as! DataSnapshot
        let dict = childSnap.value as! [String: Any]
        let cat = dict["category"] as! String
        let name = dict["name"] as! String
        print(childSnap.key, cat, name)
    }
})

Solution

  • This is a standard Firebase Query.

    To retrieve nodes that contain a child that has a certain value, we query the nodes for the node key in question and specify the value being looked for. They will be returned in a snapshot, and they can be more than one so we need to iterate over each of the returned snapshots.

    let questionPostsRef = self.ref.child("questionPosts")
    let query = questionPostsRef.queryOrdered(byChild: "trackId").queryEqual(toValue: "12345678")
    query.observeSingleEvent(of: .value, with: { snapshot in
        for child in snapshot.children {
            let childSnap = child as! DataSnapshot
            let dict = childSnap.value as! [String: Any]
            let cat = dict["category"] as! String
            let name = dict["name"] as! String
            print(childSnap.key, cat, name)
        }
    })
    

    and the output would be (assume two were found)

    -Lu9ma9jsd...  some category   some name
    -K999a90k9...  some category   some name
    

    This is covered in the Firebase guide: Work With Lists Of Data in the Sorting and Filtering section

    Now, the question states

    And I want find the trackId that has the same value as my variable and then get that trackId

    Of course if you know the trackId already you wouldn't need to fetch it since you already have it. My guess is that you want to return the nodes that match that track ID so you can get to the other data, so that's what the above code does. If you are asking something different, let me know.

    EDIT: a followup for the OP. To configure Firebase, this is the typical design pattern within a view controller. From there, you can reference your Firebase throughout this class with self.ref (as in my answer above)

    import UIKit
    import Firebase
    import FirebaseAuth
    import FirebaseDatabase
    import FirebaseStorage
    
    class ViewController: UIViewController {
        var ref: DatabaseReference!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.ref = Database.database().reference()