首页 > 解决方案 > Swift:如何根据 Firebase 中的用户 ID 读取特定的用户数据?

问题描述

我已经研究了几天,但我无法找到解决方案。我正在使用 firebase 数据库作为我的应用程序的后端。如何使用唯一用户 ID 作为读取特定用户数据的一种方式?我有一个用户列表,每个用户都有自己的数据,现在的问题是,当我选择其中一个用户时,我会看到所有用户的数据,而不仅仅是我选择的用户。我希望能够通过从 tableview 中选择来查看每个用户数据。有什么帮助吗?

import UIKit
import Firebase
import FirebaseDatabase

class detailsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    //connecting table view
    @IBOutlet weak var tableViewCards: UITableView!

    // defining firebase reference var
    var ref: DatabaseReference!

    // list to store card details
    var cardList = [CardModel]()

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

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // creating a cell using the custom class
        let cell = tableViewCards.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

        // the card object
        let card: CardModel

        //getting the name
        card = cardList[indexPath.row]
        //adding values to labels
        cell.labelName.text = card.name
        cell.labelCompanyName.text = card.company
        cell.labelRole.text = card.role
        cell.labelMobile.text = card.mobile
        cell.labelPhone.text = card.telephone
        cell.labelEmail.text = card.email
        cell.labelAddress.text = card.address

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // getting a reference to the database
        ref = Database.database().reference()

        // observing data changes
        ref.observe(DataEventType.value, with: {(snapshot) in

            // if the reference has values
            if snapshot.childrenCount > 0 {

                //clearing the list
                self.cardList.removeAll()

                //iterating through all the values
                for cards in snapshot.children.allObjects as! [DataSnapshot] {
                    //getting values
                    let cardObject = cards.value as? [String: AnyObject]
                    let personName = cardObject?["name"]
                    let cardId = cardObject?["id"]
                    let companyName = cardObject?["company"]
                    let roleName = cardObject?["role"]
                    let mobileNo = cardObject?["mobile"]
                    let phoneNo = cardObject?["telephone"]
                    let emailAdd = cardObject?["email"]
                    let compAdd = cardObject?["address"]

                    //creating card object with model and values
                    let card = CardModel(id: cardId as! String?, name: personName as! String?, company: companyName as! String?, role: roleName as! String?, mobile: mobileNo as! String?, telephone: phoneNo as! String?, email: emailAdd as! String?, address: compAdd as! String?)

                    //appending it to the list
                    self.cardList.append(card)
                }
                //reloading to the tableView
                self.tableViewCards.reloadData()
            }
        })
    }
}

数据库结构:

{
  "-LOGJARYHpNDqiReJGP3" : {
    "address" : "10 test st test ",
    "company" : "grated",
    "email" : "test@email.com",
    "id" : "-LOGJARYHpNDqiReJGP3",
    "mobile" : "0412345678",
    "name" : "Jason",
    "role" : "engineer",
    "telephone" : "02999999"
  }
}

标签: iosswiftfirebase

解决方案


无法为您提供确切的代码,因为您没有指定 Firebase 数据库的结构。

但是您需要在以下方面进行更改:

ref = Database.database().reference()

当用户登录时,您可以通过 Auth 类访问应用程序中的 uid。

ref = Database.database().reference().child("Users").child(user.uid)

由于您已将所有内容放入根节点,您可以尝试:

ref = Database.database().reference().child(user.uid)

您将需要查看您的数据库,将内容分离到其他节点中,否则以后安全性将成为您的问题。


推荐阅读