gpt4 book ai didi

ios - Swift:如何在firebase中根据用户id读取特定用户数据?

转载 作者:行者123 更新时间:2023-11-30 11:05:53 25 4
gpt4 key购买 nike

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

最佳答案

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

但是您需要进行更改:

ref = Database.database().reference()

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

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

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

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

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

关于ios - Swift:如何在firebase中根据用户id读取特定用户数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52732842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com