Firebase Swift iOS - 从孩子身上获取价值

时间:2017-12-10 07:41:11

标签: ios swift firebase

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

1 个答案:

答案 0 :(得分:0)

已解决

我阅读了文档,并对它应该如何工作进行了一些测试,我得出了这个答案。我需要做的是改变我处理器的位置。我还改变了我的数据库结构,以便更容易拉动。下面是一个关于如何在数据库中提取特定值的简单代码。

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