【问题标题】:print out firebase child values打印出firebase子值
【发布时间】:2017-10-11 02:30:55
【问题描述】:

我想查看 Firebase 并检查某个孩子的某个值是否存在,以及该值是否不存在。我想打印一份声明

 databaseRef.child("books").queryOrdered(byChild: "Category").queryEqual(toValue: categorySegue).observe(.childAdded, with: { (snapshot) in

            if snapshot.exists() {
                print("data found")
            }else{
                print("no data found")
            }
})

当子值存在时,它会打印出完全正常但不存在的数据。它不会打印出没有找到数据

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    那是因为您正在观察 .childAdded,它仅在存在与您的查询匹配的孩子时才会触发。

    如果您还想检测没有子匹配的情况,则需要观察.value 事件并循环遍历结果。

    databaseRef.child("books").queryOrdered(byChild: "Category").queryEqual(toValue: categorySegue).observe(.childAdded, with: { (snapshot) in
    
        if snapshot.exists() {
            print("data found")
            for child in snapshot.children {
                print(child.key)
            }
        }else{
            print("no data found")
        }
    
    })
    

    另见Searching through child values Firebase / Swift

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 2019-05-09
      • 1970-01-01
      • 2017-11-12
      • 2013-11-14
      相关资源
      最近更新 更多