Search code examples
swiftuitableviewgoogle-cloud-firestoreuipickerview

after filtering data from Firestore using a PickerViewer i'm not able to send the data to another VC


i have a UIPickerViewer with 3 lines, after querying data from Firestore i want to be able to send the results to TableView in another VC. Segue works but no data is transferred. Not sure what i'm missing or what i'm doing wrong. Thank you!

i'm able to retrieve data from Firestore to a TableView directly, but, when i filter the data using the PickerView, i cannot send the results to a TableView on a separate VC.

    @IBAction func getDataTapped(_ sender: Any) {
    SVProgressHUD.show()

    if HOSP != (hospnameTxt.text!) {
        query = 
Firestore.firestore().collection(PTLIST_REF).whereField("hosp", 
isEqualTo: (hospnameTxt.text!))
    } else if TEAM != teamnameTxt.text {
        query = 
Firestore.firestore().collection(PTLIST_REF).whereField("team", 
isEqualTo: (teamnameTxt.text!))
    } else if ASSIGNEDMD != provnameTxt.text {
        query = 
Firestore.firestore().collection(PTLIST_REF).whereField("assignedmd", 
isEqualTo: (provnameTxt.text!))

    }

    query.getDocuments { (snapshot, error) in
        if let err = error {
            debugPrint("error getting data: \(err)")
        } else {
            guard let snap = snapshot else { return }
            for document in snap.documents {
                let data = document.data()
                let assigneddate = data[ASSIGNEDDATE] as? String ?? ""
                let assignedmd = data[ASSIGNEDMD] as? String ?? ""
                let officemd = data[OFFICEMD] as? String ?? ""
                let ptname = data[PTNAME] as? String ?? ""
                let room = data[ROOM] as? String ?? ""
                let seenosee = data[SEENOSEE] as? String ?? ""

                let newPtList = PTList(assigneddate: assigneddate, 
assignedmd: assignedmd, officemd: officemd, ptname: ptname, room: 
room, seenosee: seenosee)
                self.ptlists.append(newPtList)
                print(document.data())
            }
            //self.TableView.reloadData()
        }
        self.performSegue(withIdentifier: "gotoresults", sender: self)

    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "gotoresults" {
        let vc = segue.destination as! resultsdataVC
        vc.ptlists = ptlists
        SVProgressHUD.dismiss()
    }
}

resultsdataVC

class resultsdataVC: UIViewController, UITableViewDataSource, 
UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var ptlist: PTList!
var ptlists = [PTList]()

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self

}

func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int {
    return ptlists.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCell(withIdentifier: 
"ptlistCell", for: indexPath)
        as? ptlistCell {
        cell.configureCell(ptlist: ptlists[indexPath.row])
        return cell
    }
    return UITableViewCell()
  }
}

ptlistCell

class ptlistCell: UITableViewCell {

@IBOutlet private weak var ptnameLbl: UILabel!
@IBOutlet private weak var assignedmdLbl: UILabel!
@IBOutlet private weak var officemdLbl: UILabel!
@IBOutlet private weak var roomnumberLbl: UILabel!
@IBOutlet private weak var seenoseeLbl: UILabel!
@IBOutlet private weak var assigneddateLbl: UILabel!

private var ptlist: PTList!

override func awakeFromNib() {
    super.awakeFromNib()

}

func configureCell(ptlist: PTList) {

    ptnameLbl.text = ptlist.ptname
    assignedmdLbl.text = ptlist.assignedmd
    officemdLbl.text = ptlist.officemd
    roomnumberLbl.text = ptlist.room
    seenoseeLbl.text = ptlist.seenosee
    assigneddateLbl.text = ptlist.assigneddate 

}


@IBAction func ptdataTapped(_ sender: Any) {

}

Solution

  • Calling firebase inside prepare(for segue: is too late , Segue is done before data is fetched from firebase as it's asynchnous so do

    @IBAction func getDataTapped(_ sender: Any) {
       firebaseCall {
        // inside it the segue and the data is fetched successfully 
        performSegue(withIdentifier: "gotoresults", sender: self)
      }
    } 
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "gotoresults" {
            let vc = segue.destination as! resultsdataVC
            vc.data = data // set any data here 
        }
    }
    

    Edit:

    @IBAction func getDataTapped(_ sender: Any) {
    
            if HOSP != (hospnameTxt.text!) {
                query =
                    Firestore.firestore().collection(PTLIST_REF).whereField("hosp",
                                                                            isEqualTo: (hospnameTxt.text!))
            } else if TEAM != teamnameTxt.text {
                query =
                    Firestore.firestore().collection(PTLIST_REF).whereField("team",
                                                                            isEqualTo: (teamnameTxt.text!))
            } else if ASSIGNEDMD != provnameTxt.text {
                query =
                    Firestore.firestore().collection(PTLIST_REF).whereField("assignedmd",
                                                                            isEqualTo: (provnameTxt.text!))
    
            }
    
            query.getDocuments { (snapshot, error) in
                if let err = error {
                    debugPrint("error getting data: \(err)")
                } else {
                    guard let snap = snapshot else { return }
                    for document in snap.documents {
                        let data = document.data()
                        let assigneddate = data[ASSIGNEDDATE] as? String ?? ""
                        let assignedmd = data[ASSIGNEDMD] as? String ?? ""
                        let officemd = data[OFFICEMD] as? String ?? ""
                        let ptname = data[PTNAME] as? String ?? ""
                        let room = data[ROOM] as? String ?? ""
                        let seenosee = data[SEENOSEE] as? String ?? ""
    
                        let newPtList = PTList(assigneddate: assigneddate,
                                               assignedmd: assignedmd, officemd: officemd, ptname: ptname, room:
                            room, seenosee: seenosee)
                        self.ptlists.append(newPtList)
                        print(document.data())
                    }
    
                }
    
                performSegue(withIdentifier: "gotoresults", sender: self)
    
            }
        }
    
        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "gotoresults" {
                let vc = segue.destination as! resultsdataVC
                vc.ptlists = ptlists
            }
        }
    }