0

I want to know if some key exists in my database, I found this here:

DatabaseReference rootRef = 
FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
   void onDataChange(DataSnapshot snapshot) {
   if (snapshot.hasChild("name")) {
    // run some code
   }
 }
});

But my question is, is this way effective or it is better just looking for the child and check if it is not null? like this:

DatabaseReference savedItems = bDatabase.getReference().child("CHILD1").child("CHILD2");
if(savedItems == null { //do something }

Thanks in advance.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Andres Felipe
  • 4,292
  • 1
  • 24
  • 41
  • The second snippet won't work. You're not listening for anything. – Frank van Puffelen Aug 08 '17 at 17:35
  • @FrankvanPuffelen so there is not another way to do this? – Andres Felipe Aug 08 '17 at 17:36
  • Your second snippet is currently not doing anything, so it's hard to say what's more effective. That said: downloading a bunch of JSON to [check if a specific key exists](https://www.google.com/search?q=site:stackoverflow.com+firebase+check+if+key+exists) is hardly ever the most efficient way to do so. E.g. https://stackoverflow.com/questions/36826429/understanding-if-data-exists-in-firebase/36826793#36826793 – Frank van Puffelen Aug 08 '17 at 17:42
  • @FrankvanPuffelen thank you for clarifying me this. – Andres Felipe Aug 08 '17 at 22:28

0 Answers0