Checking if Firebase snapshot is equal to nil in Swift

前端 未结 2 1568
一生所求
一生所求 2020-12-06 03:28

I am trying to query Firebase to check if any user that has waiting: \"1\" and then when the snapshot is returned I want to see whether it is equal to nil. I ha

2条回答
  •  孤街浪徒
    2020-12-06 03:51

    There are a few things going on here, but the most important one is that you cannot test for the existence of children with .ChildAdded. That makes sense if you think about it: the .ChildAdded event is raised when a child is added to the location. If no child is added, the event won't be raised.

    So if you want to test if a child exists at a location, you need to use .Value. Once you do that, there are various way to detect existence. Here's one:

    ref.queryOrderedByChild("waiting").queryEqualToValue("1")
       .observeEventType(.Value, withBlock: { snapshot in
           print(snapshot.value)
           if !snapshot.exists() {
               print("test2")
           }
       });
    

提交回复
热议问题