Skip to content Skip to sidebar Skip to footer

How Getchildrencount() Works In The Background?

In my database I have lets say more than 5000 users, now if I use getChildrenCount() in the main parent node to get how many people do I have I know that getChildrenCount returns

Solution 1:

When you are using DataSnapshot's getChildrenCount() method, it doesn't mean that you are looping through the entire DataSnapshot object. Instead, if you are using DataSnapshot's getChildren() method you are precisely doing that. Let's take an example, let's say you have a node named users which holds as you say, over 5000 users.

DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReferenceusersRef= rootRef.child("users");
ValueEventListenervalueEventListener=newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        longnumberOfUsers= dataSnapshot.getChildrenCount(); // Count the number of users//This is called iterationfor(DataSnapshot ds : dataSnapshot.getChildren()) {
            // Get user object on every iteration
        }
    }

    @OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(valueEventListener);

Because every node in a Firebase database is a Map, DataSnapshot can also be called a Map.

A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.

So if you want to cast a DataSnapshot object to a Map, the following line of code will work perfectly fine:

Map<String, Object> map = (Map<String, Object>) dataSnapshot;

So calling getChildrenCount() on a DataSnapshot object is like calling size() method on a Map, which internally counts the number of objects it contains.

I'm wasting read resources

This is available in case of the new Cloud Firestore database, where you are charged according to the number of CRUD operations you perform against the Firestore database. If you are interested, this is how you can count the number of documents beneath a collection in Cloud Firestore. As you can see, beside the classic count opertion +=, we also have task.getResult().size(). I also have mentioned that the best practice to store counters is to store them in Firebase real-time database. So in my opinion, go ahead with that and store them separately.

If you think that your app will grow very fast, another way would be to use Cloud Functions to update the user counter every time you add or delete a users from the your users node. This tehnique works very well for big datasets.

And to answer your questions:

Does getChildrenCount() stores somewhere the latest retrieved number of my users ? Does getChildrenCount() executes each time I run my app to count with += in each child ?

If you are thinking that the latest retrieved number is stored somehow on the disk/memory, no. Everytime you are opening the app, getChildrenCount() method will be called to get the latest number of users. If you want to have that number in real-time, then you should attach a listener on users reference.

Post a Comment for "How Getchildrencount() Works In The Background?"