1

Explanation -

DatabaseReference Ref;
//intialize Ref variable
Ref = FirebaseDatabase.getInstance().getReference();  //root reference

after this, adding the valueEventListener to Ref

Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.hasChild("abcd")) {
                //abcd child is present
            }else {
                //abcd child is not present
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Now Specifically my question is which algorithm does the firebase using behind the dataSnapshot.hasChild("abcd") ?

In Firebase-database if my root reference contains a huge number of childs then this is an efficient method to use or not?

Vaibhav Miniyar
  • 743
  • 1
  • 7
  • 17

1 Answers1

1

A DataSnapshot is an efficiently-generated immutable copy of the data at a Firebase Database location. It can't be modified and will never change.

The hasChild(key) can be considered a convenience method for child(key).exists(). As keys are always unique, there is no need to iterate the entire snapshot to locate a specific key, and therefore performance should be something similar to a HashMap at O(1).

If you do have a huge amount of data though, it is often unnecessary to download everything at once, so it's recommended to filter or restrict your query, or select a deeper node and then only obtain a subset of data at a time.

For example, you could listen lower in the tree, for the abcd node directly, and then use the exists() method instead to check for the existence of a child node:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

ref.child("abcd").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            DataSnapshot child = dataSnapshot.child("efgh");
            if (child.exists()) {
                //efgh child is present
            } else {
                //efgh child is not present
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {  }
    });
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
  • 2
    Great answer @Grimthorr! You're correct that `hasChild()` can be expressed as a `exists()` (see for example the JavaScript implementation of [`hasChild()`](https://github.com/firebase/firebase-js-sdk/blob/master/packages/database/src/api/DataSnapshot.ts#L113) and [`exists()`](https://github.com/firebase/firebase-js-sdk/blob/master/packages/database/src/api/DataSnapshot.ts#L78)). The latter is preferred, since it retrieves less data. Btw: it would be great if you could expand your answer with an example of OPs code, but then using `exists()` and listening lower in the tree. – Frank van Puffelen Oct 22 '17 at 15:57
  • 1
    @FrankvanPuffelen Thanks very much Frank, glad my assumption was correct. I've updated the answer to remove the uncertainty, added an example and clarified it a little. – Grimthorr Oct 22 '17 at 17:08