Firebase实时数据库和Xcode:要允许每个用户读取自己的数据,而不是所有数据 [英] Firebase Realtime Database and Xcode: To allow each user to read their own data NOT all the data

查看:19
本文介绍了Firebase实时数据库和Xcode:要允许每个用户读取自己的数据,而不是所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将其添加到我的代码中:

I have added this to my code:

let userID = Auth.auth().currentUser!.uid
ref = Database.database().reference().child(userID);

我认为这会行得通,并为我提供了所需的结果,因为我的规则是正确的,但是该应用程序只是提供了一个空白表,而不是在其中填充了详细信息.真的很奇怪!

Thinking this would work and give me the results I needed, as my rules are right, however the app just comes up with a blank table instead of it filled in with the details. Which is really weird!

首先,这是我的Firebase规则:

Firstly here are my firebase rules:

  {
  "rules": {
    "jobs": {
      "$uid": {
        ".read": "auth.uid === $uid"
      }
    }
  }
}

这是我的Firebase数据库:

Here is my firebase database:

FirebaseDatabase

这是具有UID的Firebase身份验证

Here is the Firebase Auth with the UID

FirebaseAuth

这是我的代码:

用于登录页面的ViewController:

import UIKit
import Firebase
import FirebaseAuth

class LoginViewController: UIViewController {

    var ref: DatabaseReference!

    static var isAlreadyLaunchedOnce = false

    @IBOutlet weak var txtemail: UITextField!

    @IBOutlet weak var txtpass: UITextField!


    var isSignin:Bool = true

    override func viewDidLoad() {
        super.viewDidLoad()

       if FirebaseApp.app() == nil {
        FirebaseApp.configure()
       }
    }


    @IBAction func submit(_ sender: UIButton) {

        if let email = txtemail.text, let passowrd = txtpass.text

        { Auth.auth().signIn(withEmail: email, password: passowrd) { (user, error) in
                if user != nil {
                    self.performSegue(withIdentifier: "goto", sender : self)
                }
                else {
                    let alert = UIAlertController(title: "Username or Password Incorrect", message: nil, preferredStyle: .alert)
                    let okButton = UIAlertAction(title: "Ok", style: .default, handler: nil)
                    alert.addAction(okButton)
                    self.present(alert, animated: true, completion: nil)
                }
            }
        }
}
}

用于登录页面的视图控制器

import UIKit
import Firebase

class LoggedInViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var loginlbl: UILabel!

    @IBOutlet weak var tbl: UITableView!

    var ref: DatabaseReference!
    var jobList = [JobModel]()

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let job = jobList[indexPath.row]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

        let job: JobModel

        job = jobList[indexPath.row]

        cell.lblCol.text = job.collection
        cell.lblDel.text = job.delivery
       cell.lblShip.text = job.shipper
        cell.lblCon.text = job.consignee
       cell.lblEmai.text = job.email
       cell.lblRef.text = job.reference
       cell.lblFreight.text = job.freight
       cell.collected.text = job.collected
       cell.delivered.text = job.delievered

        return cell  }


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

override func viewDidLoad() {

        super.viewDidLoad()

if FirebaseApp.app() == nil {
FirebaseApp.configure()     }


ref = Database.database().reference().child("jobs");

ref.observe(DataEventType.value) { (snapshot) in

if snapshot.childrenCount>0 {

self.jobList.removeAll()

for jobs in snapshot.children.allObjects as! [DataSnapshot]{
    let jobObject = jobs.value as? [String: AnyObject]
    let jobId = jobObject?["id"]
    let jobShipper = jobObject?["shipper"]
    let jobConsignee = jobObject?["consignee"]
    let jobEmail = jobObject?["email"]
    let jobReference = jobObject?["reference"]
    let jobFreight = jobObject?["freight"]
    let jobCollection = jobObject?["collection date"]
    let jobDelivery = jobObject?["delivery date"]
    let jobPod = jobObject?["pod"]
    let jobCollected = jobObject?["collected"]
    let jobDelivered = jobObject?["delivered"]

    let job = JobModel(id: jobId as! String?,
            shipper: jobShipper as! String?,
            consignee: jobConsignee as! String?,
            email: jobEmail as! String?,
            reference: jobReference as! String?,
            freight: jobFreight as! String?,
            collection: jobCollection as! String?,
            delivery: jobDelivery as! String?,
            pod: jobPod as! String?,
            collected: jobCollected as! String?,
            delivered: jobDelivered as! String?)

            self.jobList.append(job)
}
            self.tbl.reloadData()
} } }


override func viewDidAppear(_ animated: Bool) {

    Auth.auth().currentUser != nil; do {

        self.loginlbl.text = "Hello " + (Auth.auth().currentUser?.email)!


    }}



}

推荐答案

您的规则可让您访问Jobs的子节点.像 jobs/user_id_1

Your rules let you access to the children node of jobs. like jobs/user_id_1

所以您必须听孩子的话,而不是整个 jobs 节点

So you have to listen to the child instead of the whole jobs node

Database.database().reference().child("jobs");

Database.database().reference().child("jobs \ loggedInUserID");

这篇关于Firebase实时数据库和Xcode:要允许每个用户读取自己的数据,而不是所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆