Nat
Nat

Reputation: 1762

How to get a specific string value out of a json response from firebase

I have this data structure and I can't extract the right value:

users
    private
        userID
            birthday: "birthdayValue"
            username: "nathan"
            firstName: "Nathan"
            etc...

I'm making a search feature in my app to search for users via their username through the firebase realtime database:

let reference = Database.database().reference()
    if(searchText != ""){
        reference.child("users").child("private").queryOrdered(byChild:  "username").queryStarting(atValue: searchText).queryEnding(atValue: searchText + "\u{f8ff}").observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.value is NSNull{
                    //handles errors
                    return
                }
                else{
                if let user = snapshot.value as? NSDictionary {
                    for child in user{
                        print(child.key)
                        print(child.value)
                    }
                    }
                    else{
                        //null
                    }
                }
            })

at the moment the two print statements are printing these two results in the console every time I search:

wnszfmHilqNl6PG9khWtWkKUPtF3
{
    birthday = 100;
    dateCreated = "1579543450313.94";
    description = nil;
    email = "[email protected]";
    firstName = Nathan;
    instagramLink = nil;
    lastLogin = "1579543450313.988";
    lastName = Ellis;
    profilePicURL = "url";
    twitchLink = nil;
    username = nathan;
    youtubeLink = nil;
}

Which is expected, it prints the usersID (the key) and the value of the snapshot as a NSDictonary. I'm only interested in getting the username, nothing else. How would I extract the username out of this firebase snapshot so I can add their username as a string to an array for my search controller?

Obviously it needs to be dynamic as the userID will always be different.

Would I need to change my data model?

Upvotes: 0

Views: 251

Answers (2)

Juri Noga
Juri Noga

Reputation: 4391

Your child.value seems to be a dictionary as well, so you can access it by:

if let valueDict = child.value as? [String: AnyObject] {
  if let username = valueDict["username"] as? String {
      // append username to results
      print(username)
  }

}

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599061

To print just the username, the smallest possible change is:

print(resultsLocalArray["username"])

This will fine, but will still retrieve the entire user node to the client, which uses more bandwidth than strictly needed.

If you find yourself frequently needing just the username of a user, or maybe even a list of username values across users, you might want to consider storing a node with just user names. So something like:

users
    userID: "nathan"

But in your current setup you only retrieve the node for a single user, so I doubt the bandwidth savings are worth the additional complexity.

Upvotes: 0

Related Questions