Firebase DB: Check if node has no children?

前端 未结 2 1640
情深已故
情深已故 2020-12-19 04:34

How do I find out if a Firebase DB node has no children? Seems the only way to get data is with listeners, and they only fire when something is added or removed. In other wo

2条回答
  •  情话喂你
    2020-12-19 05:15

    You can check whether the child is present in Firebase or not by using the getChildrenCount() or exists() method of DataSnapshot.

     searchFirebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
    
            Log.d("FIREBASE",String.valueOf(dataSnapshot.getChildrenCount()));
            String childrenCount = String.valueOf(datasnapshot.getChildrenCount());
    
           if(childrenCount != null){
    
              }else{
    
              //No childrens in Firebase Database
           }
    
    //OR 
    
           if(!dataSnapshot.exists()){
    
              //No data   
    
             }
    
    
          }
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
    
            }
    
        });
    

    See this doc for more info. I hope this helps you.

提交回复
热议问题