更新时间:2023-02-04 09:23:11
.childAdded
一次提供FIRDataSnapshot
...因此无需为此循环..您只需要在结构中传递当前子级
.childAdded
gives FIRDataSnapshot
at a time ... so no need to loop for this .. you just need to pass the current child in your structure.
self.productsArray.removeAll()
var newPostsArray = [Product]()
let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
ref.observe(FIRDataEventType.childAdded, with: { (posts) in
let newPost = Product(snapshot: posts as! FIRDataSnapshot)
newPostsArray.insert(newPost, at: 0)
self.productsArray = newPostsArray
self.tableView.reloadData()
}) { (error) in
print(error.localizedDescription)
}