gpt4 book ai didi

swift - Firebase swift - 如何根据特定唯一键列表打印出值(存储在数组中的唯一键)

转载 作者:行者123 更新时间:2023-11-30 10:44:05 25 4
gpt4 key购买 nike

//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()


}
})

最佳答案

问题有点模糊,但我认为您正在尝试根据存储在数组中的 ID(键)加载特定产品?

如果是这样,你不能直接这样做

这个

.queryEqual(toValue: ArrproductID)

将不起作用,因为 ArrproductID 是一个数组,而 .queryEqual 仅排除单个值。

Firebase 实时数据库没有“IN”查询。为此,您需要迭代数组中的每个值并检索产品。

假设您的结构如下所示

Products
product_0
ProductName: "Shampoo"
ProductPrice: "$1.99"
ProductImage: "some url"
product_7
ProductName: "Milk"
ProductPrice: "$2.99"
ProductImage: "another url"

然后是读取存储在数组中的特定产品的代码

func getProducts() {
let productArray = ["product_0", "product_7"]
let productsRef = self.ref.child("Products")

for productId in productArray {
let thisProductRef = productsRef.child(productId)
thisProductRef.observeSingleEvent(of: .value, with: { snapshot in
let name = snapshot.childSnapshot(forPath: "ProductName").value as? String ?? "No Name"
let price = snapshot.childSnapshot(forPath: "ProductPrice").value as? String ?? "$0.00"
let image = snapshot.childSnapshot(forPath: "ProductImage").value as? String ?? "No Image"
print(name, price, image)
})
}
}

输出是

Shampoo $1.99 some url
Milk $2.99 another url

请记住,Firebase 通常不建议在紧密循环中迭代观察。这可能会阻塞主线程并阻止闭包更新 UI。如果它是一个小数据集,那么它应该不是问题。

如果数据集较大,这里可能有更好的方法来构建数据以获得您想要的结果。例如,如果您想获取所有护发产品,则向每个子项添加一个字段,例如“ProductCategory”,值为“Hair Care”,这样您就可以轻松查询所有护发产品的“Products”节点。

关于swift - Firebase swift - 如何根据特定唯一键列表打印出值(存储在数组中的唯一键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56174040/

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