How do I check if a firebase database value exists?

后端 未结 6 874
野趣味
野趣味 2020-11-28 05:36

I\'m using the Realtime Database with Google\'s Firebase, and I\'m trying to check if a child exists.

My database is structured as the following

- /          


        
6条回答
  •  时光说笑
    2020-11-28 06:39

    While the answer of @ismael33 works, it downloads all the rooms to check if room1 exists.

    The following code accomplishes the same, but then only downloads rooms/room1 to do so:

    ref = FIRDatabase.database().reference()
    
    ref.child("rooms/room1").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists(){
            print("true rooms exist")
        }else{
            print("false room doesn't exist")
        }
    }) 
    

提交回复
热议问题