Search code examples
swiftgoogle-cloud-firestoreuipickerview

I can't query Firestore using a UIPickerView with 3 options


I'm trying to query Firestore using a UIPickerView with 3 fields and nothing is showing. If i comment-out 2 of the fields and run the query, i get results. Any idea what i'm missing or doing wrong. Please help.

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
 if hospNameField.isFirstResponder {
     let itemselected = hospt[row]
     hospNameField.text = itemselected
 } else if teamNameField.isFirstResponder {
     let itemselected = team[row]
     teamNameField.text = itemselected
 } else if mdNameField.isFirstResponder {
     let itemselected = assignedmd[row]
     mdNameField.text = itemselected
 }

}

 @IBAction func getData(_ sender: Any) {
 if HOSP != (hospNameField.text!) {
     query = Firestore.firestore().collection(PTLIST_REF).whereField("hosp", isEqualTo: (hospNameField.text!))
 } else if TEAM != (teamNameField.text!) {
     query = Firestore.firestore().collection(PTLIST_REF).whereField("team", isEqualTo: (teamNameField.text!))
 } else if ASSIGNEDMD != (mdNameField.text!) {
     query = Firestore.firestore().collection(PTLIST_REF).whereField("assignedmd", isEqualTo: (mdNameField.text!))
     query.getDocuments { (snapshot, error) in
         if let err = error {
             debugPrint("error getting data: \(err)")
         } else {
             for document in (snapshot?.documents)! {
                 print(document.data())
             }

Solution

  • I think that you are missing a bracket. if you run the query you execute only the third if statement.You should close the last else if and then run the query.

    Try this:

       @IBAction func getData(_ sender: Any) {
     if HOSP != (hospNameField.text!) {
         query = Firestore.firestore().collection(PTLIST_REF).whereField("hosp", isEqualTo: (hospNameField.text!))
     } else if TEAM != (teamNameField.text!) {
         query = Firestore.firestore().collection(PTLIST_REF).whereField("team", isEqualTo: (teamNameField.text!))
     } else if ASSIGNEDMD != (mdNameField.text!) {
         query = Firestore.firestore().collection(PTLIST_REF).whereField("assignedmd", isEqualTo: (mdNameField.text!))
    }
         query.getDocuments { (snapshot, error) in
             if let err = error {
                 debugPrint("error getting data: \(err)")
             } else {
                 for document in (snapshot?.documents)! {
                     print(document.data())
                 }
               }
           }
    }