Firebase queryOrderedbyChild doesn't return a Null value

前端 未结 1 670
误落风尘
误落风尘 2021-01-28 01:36

I have a query that searches for users based on their age:

self?.ref.child(\"users\").queryOrdered(byChild: \"age\").queryStarting(atValue: \"18\").queryEnding(a         


        
1条回答
  •  生来不讨喜
    2021-01-28 02:21

    In the data you show there are no nodes with 19 or higher, so the query doesn't match any nodes. In that case the .childAdded event does not fire.

    If you want to detect this condition, you must listen to the .value event, which does fire even when there are no children. But you'll get a single .value event for all matching children in this case, so you'll need to loop over the child nodes:

    self?.ref.child("users")
      .queryOrdered(byChild: "age").queryStarting(atValue: "18")
      .queryEnding(atValue: "25")
      .observeSingleEvent(of: .value, with:  { (snapshot) in
        print(snapshot.exists())
        for child in snapshot.children {
          ...
        }
    

    0 讨论(0)
提交回复
热议问题