I’m working on this app that runs 2 different queries to Firebase. The first Firebase query displays the data inside a pickerView, the second is a query to a “users” collection and pulls a custom field. Both queries are working as expected, pickerView displays the info and I’m able to print the results of the user’s collection to the console. The issue I’m having is that I’m trying to pass the value inside the custom field userSpecialtyCode to the next VC and is not working. I'm not sure what I'm missing. Any help is greatly appreciated. Here is my code:
class PatfilterVC: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var hospNameTxt: UITextField!
@IBOutlet weak var getDataBtn: UIButton!
@IBOutlet weak var infoPickerViewer: UIPickerView!
private var textFieldSelected = UITextField()
private var pickerView: UIPickerView?
private var handle: AuthStateDidChangeListenerHandle?
private var userListener: ListenerRegistration!
private var hospitalClass = [HospitalList]()
private var hospCollectionRef: CollectionReference!
private var hospCode: HospitalList! = nil
private var loggedInUserClass = [UserSpecialty]()
private var usersCollectionRef: CollectionReference!
private var currentUserSpecialty: UserSpecialty! = nil
override func viewDidLoad() {
super.viewDidLoad()
overrideUserInterfaceStyle = .light
let pickerView = UIPickerView()
infoPickerViewer.delegate = self
infoPickerViewer.dataSource = self
hospNameTxt.inputView = pickerView
hospNameTxt.delegate = self
self.infoPickerViewer = pickerView
self.infoPickerViewer?.delegate = self
self.infoPickerViewer?.dataSource = self
self.infoPickerViewer?.reloadAllComponents()
hospCollectionRef = Firestore.firestore().collection(HOSPITAL_REF)
usersCollectionRef = Firestore.firestore().collection(USERS_REF)
handle = Auth.auth().addStateDidChangeListener({ (auth, user) in
if user == nil {
self.getDataBtn.isUserInteractionEnabled = false
self.infoPickerViewer.isUserInteractionEnabled = false
} else {
self.getDataBtn.isUserInteractionEnabled = true
self.infoPickerViewer.isUserInteractionEnabled = true
self.setListener()
}
})
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
getHospitalList()
getUserSpecialty()
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
override func viewWillAppear(_ animated: Bool) {
handle = Auth.auth().addStateDidChangeListener({ (auth, user) in
if user == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let LoginVC = storyboard.instantiateViewController(withIdentifier: "LoginVC")
self.present(LoginVC, animated: true, completion: nil)
} else {
self.setListener()
}
})
}
override func viewWillDisappear(_ animated: Bool) {
if userListener != nil {
userListener.remove()
}
}
func setListener() {
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
self.pickerView?.reloadAllComponents()
return true
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return hospitalClass.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return hospitalClass[row].hospitalName
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
hospCode = hospitalClass[row]
let hosp = hospitalClass[row].hospitalName
hospNameTxt.text = hosp
}
@IBAction func getDataTapped(_ sender: Any) {
guard hospCode != nil else {return} //I'm able to pass this to ResultsdataVC
guard currentUserSpecialty != nil else {return} //I'm not able to pass, value returns nil
performSegue(withIdentifier: "goToResults", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToResults" {
let vc = segue.destination as! ResultsdataVC
if let hosp = hospCode {
vc.hospCodeFromVC = hosp.hospitalCode
}
if let user = currentUserSpecialty {
vc.userSpecFromVC = user.userSpecialtyCode
}
}
}
//this query displays in the pickerView
func getHospitalList() {
let hospListQuery = hospCollectionRef?
.order(by: HOSPITAL_NAME, descending: false)
hospListQuery?.getDocuments { (snapshot, error) in
if let err = error {
debugPrint("error fetching docs: \(err)")
} else {
self.infoPickerViewer.reloadAllComponents()
let snap = snapshot
for document in snap!.documents {
let data = document.data()
let hospitalCode = data[HOSPITAL_CODE] as? String ?? ""
let hospitalName = data[HOSPITAL_NAME] as? String ?? ""
let hospList = HospitalList(hospitalCode: hospitalCode, hospitalName: hospitalName)
self.hospitalClass.append(hospList)
}
}
}
}
//this query gets the values from Firebase but i cannot pass userSpecialtyCode
// to the next viewController
func getUserSpecialty() {
if let loggedInUser = Auth.auth().currentUser?.uid {
let docRef = usersCollectionRef.document(loggedInUser)
docRef.getDocument { ( document, error) in
if let document = document, document.exists {
let data = document.data()
let userSpecialtyCode = data?[SPECIALTY_CODE] as? String ?? ""
let loggedInUserId = data?[LOGGED_IN_USER_ID] as? String ?? ""
let loggedUser = UserSpecialty(userSpecialtyCode: userSpecialtyCode, loggedInUserId: loggedInUserId)
self.loggedInUserClass.append(loggedUser)
print("specCode", userSpecialtyCode as Any) //IT PRINTS THE CORRECT VALUE
print("loggedInUser", loggedInUserId as Any) //IT PRINTS THE USER ID
} else {
print("no document")
}
}
}
}
@IBAction func logoutTapped(_ sender: Any) {
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
} catch let signoutError as NSError {
debugPrint("Error signing out: \(signoutError)")
}
}
}