gpt4 book ai didi

ios - Firebase Swift iOS - 从 child 身上获取值(value)

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

我试图从一个 child 那里获取特定的值,然后从其他 child 那里获取其余的值。这就是我的数据库的样子。我想从 Bienmesabe 的键 longDescription put 中获取值。

This is how my database looks like - Click to see image

    func getCakeDescription (from category: String, subcategory: String, handler: @escaping (_ cakeDescription: [GeneralInfo]) -> ()) {
var cakeDescriptionArray = [GeneralInfo]()

let trimCategory = category.trimmingCharacters(in: .whitespaces)
let trimSubcategory = subcategory.replacingOccurrences(of: " ", with: "")

REF_CAKES.child(trimCategory).child(trimSubcategory).observeSingleEvent(of: .value) { (returnedDescriptionSnapshot) in
guard let returnedDescriptionSnapshot = returnedDescriptionSnapshot.children.allObjects as? [DataSnapshot] else {return}

for cakesDesc in returnedDescriptionSnapshot{

let cakeLongDescription = cakesDesc.childSnapshot(forPath: "longDescription").value as! String

self.REF_CAKES.child("General").observeSingleEvent(of: .value) { (returnedGeneralSnapshot) in
guard let returnedGeneralSnapshot = returnedGeneralSnapshot.children.allObjects as? [DataSnapshot] else {return}

for generalInfo in returnedGeneralSnapshot{
let titleDelivery = generalInfo.childSnapshot(forPath: "TitleDelivery").value as! String
let orderAndDelivery = generalInfo.childSnapshot(forPath: "OrderAndDelivery").value as! String
let titleSizeAndPrice = generalInfo.childSnapshot(forPath: "TitleSizeAndPrice").value as! String
let sizeAndPrice = generalInfo.childSnapshot(forPath: "SizeAndPrice").value as! String
let titlePromo = generalInfo.childSnapshot(forPath: "TitlePromo").value as! String
let promotions = generalInfo.childSnapshot(forPath: "Promotions").value as! String


let cakesInfo = GeneralInfo(titleDevilery: titleDelivery, titleSizeAndPrice: orderAndDelivery, titlePromo: titleSizeAndPrice, sizeAndDelivery: sizeAndPrice, orderAndDelivery: titlePromo, promotions: promotions, cakeDescription: cakeLongDescription)

cakeDescriptionArray.append(cakesInfo)
}
}

}
handler(cakeDescriptionArray)
}
}

最佳答案

已解决

我阅读了文档并对其如何工作进行了一些测试,然后我得出了这个答案。我需要做的是改变我放置处理程序的位置。我还更改了数据库结构以更容易提取。下面是有关如何提取数据库中特定值的简单代码。

    func getDescription (from category:String, and subcategory:String, handler: @escaping (_ cakeDescription: String)->()){

let trimSubcategory = subcategory.replacingOccurrences(of: " ", with: "")

REF_CAKES.child(category).child(trimSubcategory).observeSingleEvent(of: .value) { (cakeDescSnapshot) in
guard let cakeDescSnapshot = cakeDescSnapshot.children.allObjects as? [DataSnapshot] else { return }
for desc in cakeDescSnapshot{
if desc.key == "longDescription"{
handler(desc.value as! String)
}
}
}
}

还有其他方法,也可以使用查询方法。下面是一个例子

    func getCakeDescription (from category: String, subcategory: String, handler: @escaping (_ cakeDescription: String) -> ()) {

let trimSubcategory = subcategory.replacingOccurrences(of: " ", with: "")

REF_CAKES.child(category).child(trimSubcategory).queryOrdered(byChild: "longDescription").observeSingleEvent(of: .childAdded) { (snapshot) in
handler(snapshot.value as! String)
})
}
}

关于ios - Firebase Swift iOS - 从 child 身上获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47736853/

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