user6820041
user6820041

Reputation: 1223

Searching through child values Firebase / Swift

My database has values sorted like this :

 Users
    UID
      Username
      Email

I'm wanting to implement a friend adding system where you search for either a username or email and it lets you add the person.

I'm able to locate users by using

 REF_USERS.queryOrdered(byChild: "displayname").queryEqual(toValue: input).observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in {
 print(snapshot.value)
}

With that I get the user's entire dictionary, but I'm having an issue grabbing the UID.

snapshot.key gives me "Users".

How can I grab the UID value out of the dictionary after finding the user's dictionary with either their username/email?

Upvotes: 1

Views: 6104

Answers (2)

Jay
Jay

Reputation: 35667

Try this...

Assume a structure (this is Swift 2, Firebase 2)

users
  uid_0
    email: "[email protected]"
    displayName: "some display name"

and we want to get uid_0's info

let displayName = "some display name"

usersRef.queryOrdered(byChild: "displayName").queryEqual(toValue: displayName)
              .observeSingleEvent(of: .childAdded) { (snapshot: FIRDataSnapshot) in {

     let dict = snapshot?.value as! [String: AnyObject]
     let email = dict["email"]
     let displayName = dict["displayName"]
     print(email!)
     print(displayName!)

     let key = snapshot?.key
     print(key!)
}

A couple things to note

The 'dict' variable is being told it's being assigned a dictionary of type [String: Anyobject].

Any Object could well, be any object. A String, another dictionary, an int. So you need to ensure you code can handle whatever the object is

The snapshot key in this case is the snapshot of this user, and the key must be the parent node, which in this case is uid_0. So the output is

[email protected]
some display name
uid_0

EDIT:

Updated for Firebase 4, Swift 4 and handle the case where multiple children are returned

let usersRef = self.ref.child("users")

let input = "some display name"
let query = usersRef.queryOrdered(byChild: "displayName").queryEqual(toValue: input)
query.observeSingleEvent(of: .value, with: { snapshot in
    for child in snapshot.children {
        let snap = child as! DataSnapshot
        let dict = snap.value as! [String: Any]
        let email = dict["email"] as! String
        let displayName = dict["displayName"] as! String
        print(email)
        print(displayName)

        let key = snapshot.key
        print(key)
    }
})

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 599956

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

let query = REF_USERS.queryOrdered(byChild: "displayname").queryEqual(toValue: input)
query.observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in {
    for child in snapshot.children {
        print(child.key)
    }
}

Also see:

Upvotes: 1

Related Questions