2021年1月22日星期五

retrieve data for specific user swift firestore

I have data in firestore collections which is (forJohn) and the second one (forOlivia) and i want to get it in my application , i used the code in example 1 and it works 100% but i want if John logged in to his account get document (forJohn) and if Olivia logged in to her account i want to get (forOlivia) collection the code in example number 1 works, i tried the code in example 2 but it cannot get data from firestore . is there alternative way than using if statement ?

example 1 code

 import UIKit   import FirebaseFirestore   import Firebase   import FirebaseAuth     class countryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {      @IBOutlet var country: UITableView!  @IBOutlet var userNameField: UITextField!  var db: Firestore!  var Area = [String]()      override func viewDidLoad() {      super.viewDidLoad()                        country.register(UINib(nibName: "countryTableViewCell", bundle: nil) , forCellReuseIdentifier: "country")            country.separatorStyle = .none      country.dataSource = self      country.delegate = self      db = Firestore.firestore()      self.navigationItem.setHidesBackButton(true, animated: true)      getDoc()      getDoc2()              }              func getDoc() {            db.collection("forJohn").getDocuments() {          (querySnapshot, err) in                    if let err = err          {              print("Error getting documents: \(err)");          }          else          {                            for document in querySnapshot!.documents {                  self.Area.append(document.get("area") as? String ?? "")              }          }          self.country.reloadData()      }  }          func getDoc2() {            db.collection("forOlivia").getDocuments() {          (querySnapshot, err) in                    if let err = err          {              print("Error getting documents: \(err)");          }          else          {              for document in querySnapshot!.documents {                  self.Area.append(document.get("area") as? String ?? "")              }                        }          self.country.reloadData()      }    }  }  

example 2 code

 import UIKit   import FirebaseFirestore   import Firebase   import FirebaseAuth     class countryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {      @IBOutlet var country: UITableView!  @IBOutlet var userNameField: UITextField!  var db: Firestore!  var Area = [String]()      override func viewDidLoad() {      super.viewDidLoad()                        country.register(UINib(nibName: "countryTableViewCell", bundle: nil) , forCellReuseIdentifier: "country")            country.separatorStyle = .none      country.dataSource = self      country.delegate = self      db = Firestore.firestore()      self.navigationItem.setHidesBackButton(true, animated: true)               if userNameField.text == "John" {                    getDoc()                } else if userNameField.text == "Olivia" {                    getDoc2()                }                         }              func getDoc() {            db.collection("forJohn").getDocuments() {          (querySnapshot, err) in                    if let err = err          {              print("Error getting documents: \(err)");          }          else          {                            for document in querySnapshot!.documents {                  self.Area.append(document.get("area") as? String ?? "")              }          }          self.country.reloadData()      }  }          func getDoc2() {            db.collection("forOlivia").getDocuments() {          (querySnapshot, err) in                    if let err = err          {              print("Error getting documents: \(err)");          }          else          {              for document in querySnapshot!.documents {                  self.Area.append(document.get("area") as? String ?? "")              }                        }          self.country.reloadData()      }    }  }  
https://stackoverflow.com/questions/65855931/retrieve-data-for-specific-user-swift-firestore January 23, 2021 at 01:06PM

没有评论:

发表评论