Leonardo Martins
Leonardo Martins

Reputation: 23

Is it possible to have a completion block on the firestore query?

I currently have a Firestore database reference that queries the data trying to find a user with a certain username. After I get the user I want to try to login. However, I have noticed that the block of code to query for a user, returns as soon as it is called. Is there any way to add a completion block or at least stop the program until it is done querying.

u.name = name
global.db.collection("users").whereField("username", isEqualTo: u.name).getDocuments(completion: { (snap, error) in
    if error != nil {
        print(error?.localizedDescription as Any)
        return
    }
    for doc in (snap?.documents)! {
        u.email = doc.data()["email"] as! String
    }
})

Auth.auth().signIn(withEmail: u.email, password: password, completion: { (user, error) in
    if error != nil {
        print(error?.localizedDescription as Any)
        return
    }
    print("Succesfully Logged In")
    self.toListSelector(user: u)
})

This is the link to the image of my Firestore database https://i.sstatic.net/hI1iY.png

Upvotes: 1

Views: 654

Answers (1)

Josh Homann
Josh Homann

Reputation: 16347

You need to put your signIn INSIDE the getDocuments handler, probably after the for loop?

Upvotes: 2

Related Questions