0

For some reason that I cannot figure out, the last print statement is executing before the second print statement (doc.data()). Any help would be much appreciated. For some context, I am trying to query data from firestore and save some of that data in a User object. So why are these print statements being executed in the wrong order.

let db = Firestore.firestore()
guard let uid = Auth.auth().currentUser?.uid else { return }

print("hello world")
db.collection("users").whereField("uid", isEqualTo: uid).getDocuments() { (querySnapshot, error) in
    if let error = error {
        print("Error finding user: \(error)")
    } else {
        for document in querySnapshot!.documents {
            print(document.data())
        }
    }
}

print("Am I crazy or is Xcode?")
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

3

Watch Your logs carefully. Response from firestore is asynchronous. Probably your logs looks like this:

hello world
Am I crazy or is Xcode?
user1
user2
user3

robertbeb
  • 1,520
  • 1
  • 10
  • 13