gpt4 book ai didi

swift - 如何获取数据库中所有用户 "username"?

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

我正在为我的应用程序创建一个搜索页面,允许用户相互搜索。我遇到的问题是我只能搜索我当前登录的用户。 (注意我又创建了 8 个假用户)

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!

//let uid = Auth.auth().currentUser?.uid

var ref = Database.database().reference().child("users").child("\(Auth.auth().currentUser!.uid)")

var filteredData = [UserModel]()

override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
searchBar.delegate = self
//filteredData = data

ref.queryOrderedByValue().observeSingleEvent(of: .value) { (snapshot) in
if let userDict = snapshot.value as? [String : AnyObject] {
let userSearch = userDict["username"] as? String

self.filteredData.append(UserModel(NameDisplay: userSearch))
self.tableView.reloadData()

}
}

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = filteredData[indexPath.row].NameDisplay
return cell
}

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

}

我期望每个用户都显示在 Tableview 中,然后我就可以搜索。发生的情况是仅显示当前登录的用户

最佳答案

几件事:

您的数据库规则是否设置为允许任何经过身份验证的用户查看用户名,或者是否设置为仅允许当前经过身份验证的用户查看其用户名?

您的ref正在一直深入到当前经过身份验证的用户。因此,您的快照只会获取当前用户的用户名,因为您的引用太深了。这可能是问题所在,但是一旦进行此更改,您的数据库规则可能仍然需要调整。

关于swift - 如何获取数据库中所有用户 "username"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56941224/

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