gpt4 book ai didi

swift - 无法从 firebase 实时数据库获取/读取数据到 UITableViewController

转载 作者:行者123 更新时间:2023-11-28 07:27:50 24 4
gpt4 key购买 nike

我能够成功地将数据从我的应用程序写入我的 firebase 实时数据库。我无法检索数据并将其显示在 UIViewController 中。

我有搜索堆栈、YouTube 和 firebase 文档,但没有运气。我打印出计数并返回 0。我相信 For 循环没有迭代或者它没有正确附加到我的数组。我有 trie 强制包装和解包 refRepairs,并从中放置 databaseHandle。

    override func viewDidLoad() {
super.viewDidLoad()
refRepairs = Database.database().reference().child("repairs");

//observing the data changes
refRepairs!.child("repairs").observe(DataEventType.value, with: { (snapshot) in

//if the reference have some values
if snapshot.childrenCount > 0 {

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

//iterating through all the values
for repairs in snapshot.children.allObjects as! [DataSnapshot] {
//getting values
let repairObject = repairs.value as? [String: AnyObject]
let brand = repairObject?["brand"]
let id = repairObject?["id"]
let modelNumber = repairObject?["modelNumber"]

//creating artist object with model and fetched values
let repair = RepairModel(id: id as! String?, brand: brand as! String?, modelNumber: modelNumber as! String?)

//appending it to list
print(snapshot.childrenCount)

self.repairList.append(repair)
}

//reloading the tableview
self.doListTableView.reloadData()
}
})
}

我进行了更改并且它正在运行!!!现在,我尝试通过 uid 向数据库中添加记录,我已经成功地做到了。现在我有同样的问题,我无法显示记录。我打印了计数,它返回了 1 条正确的记录。这是新代码。

    override func viewDidLoad() {
super.viewDidLoad()
refRepairs = Database.database().reference().child("repairs");

//observing the data changes
//refRepairs!.child("uid").observe(DataEventType.value, with: { (snapshot) in


//observing the data changes
refRepairs!.observe(DataEventType.value, with: { (snapshot) in
//if the reference have some values
if snapshot.childrenCount > 0 {

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

//iterating through all the values
for repairs in snapshot.children.allObjects as! [DataSnapshot] {
//getting values
let repairObject = repairs.value as? [String: AnyObject]
let brand = repairObject?["brand"]
let uid = repairObject?["uid"]
let id = repairObject?["id"]
let modelNumber = repairObject?["modelNumber"]

//creating artist object with model and fetched values
let repair = RepairModel(uid: uid as! String?, id: id as! String?, brand: brand as! String?, modelNumber: modelNumber as! String?)

//appending it to list
print(snapshot.childrenCount)

self.repairList.append(repair)
}

//reloading the tableview
self.doListTableView.reloadData()
}
})
}

最佳答案

你在“观察数据变化”的时候,正在寻找另一个叫repairs的老二。试试这个:

override func viewDidLoad() {
super.viewDidLoad()
refRepairs = Database.database().reference().child("repairs");

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

...

如果那行不通。使用 Firebase 的 observeSingleEvent 方法

关于swift - 无法从 firebase 实时数据库获取/读取数据到 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55877060/

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