mercredi 19 octobre 2016

if-else statements executing twice inside the closure in textFieldShouldReturn function

I have a UItextField called addUserField in which a user can enter an email after the user hits enter I am checking if that email exists in my firebase db and if it does it must print "OK"\email and if does not then it should print "BAD"\email. However my code below is not doing what its intended to, it is somehow executing the if conditions twice and I am not able to figure out why.
So incase the email exists in the database it is printing:
OK xyz@abc.com
BAD xyz@abc.com
Incase the email does not exist in the database it is printing:
BAD abc@xyz.com
BAD abc@xyz.com
Can anybody help me figure out why the if statement is executing twice and how can I make it so that it just print OK if email exists or BAD if it does not.

func textFieldShouldReturn(_textField: UITextField!) -> Bool {
        var email: String = ""
        if let userEmail = addUserField.text{
        email = userEmail
        }
        let userRef = ref.child("users");
        userRef.queryOrderedByChild("role").queryEqualToValue("User").observeEventType(.Value, withBlock: {
            snapshot in
            for child in snapshot.children {
                let username = child.value["email"] as! String
                if (email == username){
                    print ("OK \(email)")
                }
                else {
                    print ("BAD \(email)")
                }
            }
        })
       return true
    }

Aucun commentaire:

Enregistrer un commentaire