gpt4 book ai didi

swift - 循环遍历 Firebase 数据会生成空表?

转载 作者:行者123 更新时间:2023-11-30 12:43:03 27 4
gpt4 key购买 nike

我正在尝试循环遍历我的 Firebase 数据库并将其全部放入一个数组中。然而,当我执行它时,数组总是空的。我该怎么做?谢谢。

categories = ["Appliances", "Arts & Crafts", "Beauty & Personal Care", "Books", "Clothing", "Electronics", "Home & Kitchen", "Movies & TV", "Sports & Outdoors", "Toys & Games", "Video Games", "Other"]

for items in categories {

let ref = FIRDatabase.database().reference(withPath: "items").child(items)

ref.observe(.value, with: { (snapshot) in

for items in snapshot.children {

let item = Item(snapshot: items as! FIRDataSnapshot)
self.allItems.append(item)

}

self.tableView.reloadData()

})

}

最佳答案

假设您的数据库已设置为此工作:

categories = ["Appliances", "Arts & Crafts", "Beauty & Personal Care", "Books", 

"Clothing", "Electronics", "Home & Kitchen", "Movies & TV", "Sports & Outdoors", "Toys & Games", "Video Games", "Other"]

for items in categories {

let ref = FIRDatabase.database().reference(withPath: "items")

ref.observe(.value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary{
for item in value.allKeys{
if let foundItem = value[item] as? String{
self.allItems.append(foundItem)
}
}
}
self.tableView.reloadData()
})
}

如果您单独声明 Firebase 引用,然后执行 ref.child("items).observe

,也可能对您有帮助

关于swift - 循环遍历 Firebase 数据会生成空表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42016275/

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