I'm trying to check if a user exists in my database but it always says "Success" no matter if the user exists or not. I don't really understand FireBase docs, they are pretty poor, can anyone help me and tell me why I get success everytime?
if nickTextField.text != "" {
let db = Database.database().reference()
var userExistsSwitch = false
db.child("Usernames").observe(.value, with: { (snapshot) in
if snapshot.hasChild("\(self.nickTextField.text!)") {
userExistsSwitch = true
print("Username already exists!")
}
})
db.child("Usernames").removeAllObservers()
if !userExistsSwitch {
print("Success!")
db.child("Usernames").child(self.nickTextField.text!).setValue(self.nickTextField.text!)
}
}