我试图遍历我的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()
})
}
答案 0 :(得分:0)
假设您的数据库设置为可以正常工作:
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