1

Android Java Firebase:

 DatabaseReference root = FirebaseDatabase.getInstance().getReference();
    DatabaseReference users = root.child("Users");
users.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                if (snapshot.childExists("name")) {
                    // run some code
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

This is my code, but childexists is not a valid working method. What is a way to check? If there is something similar can I just fix it?

adjuremods
  • 2,938
  • 2
  • 12
  • 17
august alsina
  • 197
  • 2
  • 4
  • 15
  • Possible duplicate of - http://stackoverflow.com/questions/37397205/google-firebase-check-if-child-exists – saurav Oct 16 '16 at 04:54

1 Answers1

3

Either use hasChild():

public void onDataChange(DataSnapshot snapshot) {
    if (snapshot.hasChild("name")) {
        // run some code
    }
}

Or child().exists():

public void onDataChange(DataSnapshot snapshot) {
    if (snapshot.child("name").exists()) {
        // run some code
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Im not aware how Firebase works in this regard, does the OnValueListener actually grab all the data from the database? Just wondering if this would be the right solution for a database with thousands of user entries etc. – James Heald Sep 30 '17 at 14:31