使用Firebase检索特定用户的数据

时间:2019-03-22 21:50:45

标签: swift xcode firebase firebase-realtime-database firebase-security-rules

我正在制作一个出勤应用程序,并试图从firebase数据库中为每个特定用户检索数据。有两个用户16BCA1278和16BCA1282。

这些是firebase规则,它们在firebase模拟器中起作用,但是当我尝试使用用户ID通过我的应用访问时,它说权限被拒绝。我正在尝试将值下载到每个特定的单元格,如何解决此问题?请帮助,谢谢您。

这是我的数据库:

enter image description here

enter image description here

Firebase规则:

{
   "rules":{
      "16BCA1278":{
         "$uid":{
            ".read":"'dbgrYQ6v55QskngJnTyNdYixq142' === auth.uid",
            ".write":false,

         }
      },
      "16BCA1282":{
         "$uid":{
            ".read":"'PZXe2wiCQvS90zjWcmvcMZfD1dq2' === auth.uid",
            ".write":false,

         }
      }
   }
}

这是我的快速代码:

class AttendanceViewController: UITableViewController {

@IBOutlet weak var logoutButton: UIBarButtonItem!

var calledValues = [Attendance]()

override func viewWillAppear(_ animated: Bool) {

    tableView.dataSource = self

    tableView.delegate = self

    let loginAlert = UIAlertController(title: "Welcome!", message: "", preferredStyle: .alert)

    loginAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

    self.present(loginAlert, animated: true, completion: nil)

    super.viewWillAppear(animated)

    var ref:FIRDatabaseReference!

    ref = FIRDatabase.database().reference()

    let userID = FIRAuth.auth()?.currentUser?.uid

    ref.child("uic-attendance-system-3817d").queryOrdered(byChild: "user_id").queryEqual(toValue: userID).observe(.childAdded, with: { (snapshot) in

        let results = snapshot.value as? [String : AnyObject]

        let date = results?["Date"]
        let csa = results?["Computer System Architecture"]
        let mad = results?["Mobile Application Development"]
        let mpi = results?["Microprocessor and Interfacing"]
        let st = results?["Software Testing"]
        let madlab = results?["Mobile Application Development Lab"]
        let mpilab = results?["Microprocessor and Interfacing Lab"]

        let myCalls = Attendance(date: date as! String?, csa: csa as! String?, mad: mad as! String?, mpi: mpi as! String?, st: st as! String?, madlab: madlab as! String? , mpilab: mpilab as! String?

        )

        self.calledValues.append(myCalls)

        DispatchQueue.main.async {

            self.tableView.reloadData()

        }

    }) { (error) in

        print(error.localizedDescription)

    }

}

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

    let dataCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AttendanceCell

    let values = calledValues[indexPath.row]

    dataCell.dataLabel?.text = values.date
    dataCell.csaLabel?.text = values.csa
    dataCell.madLabel?.text = values.mad
    dataCell.mpiLabel?.text = values.mpi
    dataCell.stLabel?.text = values.st
    dataCell.madlabLabel?.text = values.madlab
    dataCell.mpilabLabel?.text = values.mpilab

    return dataCell

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return calledValues.count

}

@IBAction func logoutAction(_ sender: Any) {

    let loginvc = self.storyboard!.instantiateViewController(withIdentifier: "loginvc")

    do {

        try FIRAuth.auth()?.signOut()

        self.present(loginvc, animated: true, completion: nil)

    } catch {

        print(error)

    }
}   

0 个答案:

没有答案