Swift:如何基于Firebase中的用户ID读取特定的用户数据?

时间:2018-10-10 04:53:27

标签: ios swift firebase

我已经研究了几天,但找不到解决方案。我将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"
  }
}

1 个答案:

答案 0 :(得分:0)

由于您未指定Firebase数据库的结构,因此无法提供确切的代码。

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

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

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

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

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

List 1
- Dogs
- Cats

List 2
- Pluto (Dogs)
- Oliver (Cats)
- Billo (Cats)

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