简体   繁体   English

Firebase Swift iOS-从孩子那里获得价值

[英]Firebase Swift iOS - Getting value from child

I am trying to get a specific value from a child and then get the rest from other child. 我试图从一个孩子那里得到一个特定的价值,然后从另一个孩子那里得到其余的价值。 This is how my Database looks like. 这就是我的数据库的样子。 I want to get the value from the key longDescription put of the Bienmesabe . 我想从关键值longDescription放的Bienmesabe

这是我的数据库的样子-单击以查看图片

    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)
    }
}

SOLVED 解决了

I had a read through the documentation and did some test on how it should work and I came out to this answer. 我通读了文档,并对它的工作方式进行了一些测试,然后得出了这个答案。 What I needed to do was changing where I was putting my handler. 我需要做的是更改放置处理程序的位置。 Also I changed my DB structure to be easier to pull. 另外,我还更改了数据库结构,以使其更易于拉出。 A simple code on how to extract a specific value in your database is below. 下面是一个有关如何在数据库中提取特定值的简单代码。

    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)
            }
        }
    }
}

There are other ways, you could also use the query method. 还有其他方法,您也可以使用查询方法。 An example below 下面的例子

    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)
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM