Georg Weber
Georg Weber

Reputation: 45

What is wrong in this 2 query's please

This is a query I want to do in Swift with Firestore Database. I spend a lot of time to make this code work. In debugger when it arrived in the first db.collection line the debugger jump to the second db.collection line without process the code between. After processing the 2. db.collection line he go back to the first and process the code.

   func readAirplanes() {
        var airplaneArray = [String]()
        var arrayPosition = 1
        db.collection("airplane").whereField("Userid", isEqualTo: userID).getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
                return
            } else {
                self.lbNext.isHidden = false
                self.btEdit.isUserInteractionEnabled = true
                self.btDelete.isUserInteractionEnabled = true
                for document in querySnapshot!.documents {
                    airplaneArray.append(document.documentID)
                }
            }
        }
        db.collection("airplane").document(airplaneArray[arrayPosition]).getDocument() { (document, error) in
            if let document = document, document.exists {
                self.tfPrefix.text = document.get("Prefix") as? String
                self.tfIcao.text = document.get("Icao") as? String
                self.tfModel.text = document.get("Model") as? String
                self.tfSerial.text = document.get("Serial") as? String
                self.tfManifacture.text = document.get("Manifacture") as? String
                self.tfYear.text = document.get("Year") as? String
                self.tfRules.text = document.get("Rules") as? String
                self.tfOperator.text = document.get("Operator") as? String
                self.tfCVA.text = document.get("CVA") as? String
            } else {
                print("Document does not exist")
            }
        }
    }

any help please

Upvotes: 1

Views: 74

Answers (1)

Vasia Zaretskyi
Vasia Zaretskyi

Reputation: 347

Firestore`s queries run asyncronously, not one after another. So the second query may start earlier than the first is completed.

If you want to run them one by one you need to put 2nd query into 1st.

Try this:

func readAirplanes() {
    var airplaneArray = [String]()
    var arrayPosition = 1
    db.collection("airplane").whereField("Userid", isEqualTo: userID).getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
            return
        } else {
            self.lbNext.isHidden = false
            self.btEdit.isUserInteractionEnabled = true
            self.btDelete.isUserInteractionEnabled = true
            for document in querySnapshot!.documents {
                airplaneArray.append(document.documentID)
            }


       db.collection("airplane").document(airplaneArray[arrayPosition]).getDocument()        { (document, error) in
        if let document = document, document.exists {
            self.tfPrefix.text = document.get("Prefix") as? String
            self.tfIcao.text = document.get("Icao") as? String
            self.tfModel.text = document.get("Model") as? String
            self.tfSerial.text = document.get("Serial") as? String
            self.tfManifacture.text = document.get("Manifacture") as? String
            self.tfYear.text = document.get("Year") as? String
            self.tfRules.text = document.get("Rules") as? String
            self.tfOperator.text = document.get("Operator") as? String
            self.tfCVA.text = document.get("CVA") as? String
        } else {
            print("Document does not exist")
        }
    }
        }
    }
    
}

Upvotes: 2

Related Questions