Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Retrieving Data in Swift

I'm trying to retrieve specific data from just the currently logged in user. My data in my database looks like this:

enter image description here

For example, I want to just grab the full_name and save it in a variable userName. Below is what I'm using to grab my data

ref.queryOrderedByChild("full_name").queryEqualToValue("userIdentifier").observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
            print(snapshot.value)
            // let userName = snapshot.value["full_name"] as! String
        })

Unfortunately, this is what my console prints.

enter image description here

I would appreciate any help :) Thank you!

like image 412
Tim Avatar asked Jun 11 '16 03:06

Tim


1 Answers

It gives you that warning message indexOn because you are doing a query.

you should define the keys you will be indexing on via the .indexOn rule in your Security and Firebase Rules. While you are allowed to create these queries ad-hoc on the client, you will see greatly improved performance when using .indexOn

As you know the name you are looking for you can directly go to that node, without a query.

    let ref:FIRDatabaseReference! // your ref ie. root.child("users").child("[email protected]")

    // only need to fetch once so use single event

    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

        if !snapshot.exists() { return }

        //print(snapshot)

        if let userName = snapshot.value["full_name"] as? String {
            print(userName)
        }
        if let email = snapshot.value["email"] as? String {
            print(email)
        }

        // can also use
        // snapshot.childSnapshotForPath("full_name").value as! String
    })
like image 119
DogCoffee Avatar answered Oct 08 '22 04:10

DogCoffee