0

I have the following function that observes values of my database's child node:

 func findUser() {

    let tempUser = "jordan"

    let firRef = FIRDatabase.database().reference()

    let userRef = firRef.child("user-referances")

    var username:String?

    userRef.observe(.value, with: { (snapshot) in

        if let dictionary = snapshot.value as? [String:AnyObject] {
            print(snapshot)

            print(dictionary)
            username = dictionary["username"] as? String
            //print("usernam)
            if username == tempUser {
                print("username exists")

                print(username)
                //return

            }else {
                print("username not found")
            }
        }

        }) { (error) in
            print(error)
    }
}

When I try to assign dictionary value to a string and compare them, it does not assign it. However when i print dictionary values, the values are there.

console screenshot

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
latipov15
  • 23
  • 4

2 Answers2

0

I do not know iOS development, but when I look at your console screenshot, then look at your code, I think I might see something wrong.

You use print(dictonary) and you get:

["kszzbjya8HY1IsV1Iek02FKFxtj1": {
    username = max;
}, "QW3V9o08zuele3BkfmPCM33m4K52": {
    username = jordan;
}]

Which means that the variable dictionary is equal to that entire block of data.

However, then you try to say:

username = dictionary["username"] as? String

Wouldn't you have to get each child first, and check it's username individually?

Again, I have zero knowledge of iOS or swift, so if I am way off here, let me know. But unless that line where you assign the value to username is some sort of Swift way of automatically iterating through the children of a snapshot then I think this might be your problem.

Ryan
  • 1,988
  • 4
  • 21
  • 34
0

As far as i can understand your code you are checking if a username already exists. Use this to find the username, instead of iterating through all of them.

userRef.child("user-referances").queryOrdered(byChild: "username").queryEqual(toValue: "tempUser").observeSingleEvent(of: .value, with: {(snap) in


        if let snapDict = snap.value as? [String:AnyObject] {

            for each in snapDict{

                let stringName = each.value["username"] as! String
                print("username for :\(each.key) is : \(stringName)")
            }

        }else{

            //That username doesnt exists.

        }

    })

Check :- Firebase : If username already exists.

Community
  • 1
  • 1
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • I'm not necessarily checking for current user but I'm checking if a value (lets say string value) exists in a particular child node. the screenshots shows that the value exists but I can assign it to a string variable. Your code does gives me back the particular user I'm looking for but I cannot assign dictionary value to a string or any variable. – latipov15 Sep 27 '16 at 03:35