如何根据特定的唯一键(存储在数组中的唯一键)的列表打印值
//1.检索具体id并存储在数组中
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).child("cart").observeSingleEvent(of: .value, with: { (snapshot) in
if let snapDict = snapshot.value as? [String:AnyObject]{
for each in snapDict as [String:AnyObject]{
let refID = each.value["refID"] as! String
self.ArrproductID.append(refID) //append to array
}
//print(self.ArrproductID)
}
})
{ (error) in
print(error.localizedDescription)
}
//2.根据数组中存储的id从firebase中取数
refProduct = (Database.database().reference().child("Product").queryOrderedByKey().queryEqual(toValue: ArrproductID) as! DatabaseReference)
refProduct.observe(DataEventType.value, with:{(snapshot) in
if snapshot.childrenCount>0{
self.productList.removeAll()
for Product in snapshot.children.allObjects as![DataSnapshot]{
let productObject = Product.value as? [String: AnyObject]
let ID = Product.key
let ProductName = productObject?["ProductName"]
let ProductPrice = productObject?["ProductPrice"]
let ProductImage = productObject?["ProductImage"]
//refer to the productmodel.swift? //2. store into model?
let product = ProductModel( ProductId: ID as String?, ProductName: ProductName as! String? , ProductPrice: ProductPrice as! String?, ProductImage: ProductImage as! String?)
//3. apend all product to list
self.productList.append(product)
}
//reload all the data?
self.CartItemView?.reloadData()
}
})
转载请注明出处:http://www.intsu.net/article/20230501/2252528.html