我试图从一个孩子那里得到一个特定的价值,然后再从另一个孩子那里得到其余的价值。这就是我的数据库的样子。我想从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)
    }
}

最佳答案

解决了

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

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

10-08 16:52