【问题标题】:Firebase Swift iOS - Getting value from childFirebase Swift iOS - 从孩子那里获得价值
【发布时间】:2017-12-10 07:41:11
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift firebase


    【解决方案1】:

    解决了

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2019-04-30
      相关资源
      最近更新 更多