How do I get boolean value if given string is child of given firebase location without downloading whole datasnapshot in firebase?

I need to get only true or false value, if a given String is a child of Firebase node, without getting the whole DataSnapshot of the whole node. This is for reducing the cost of Firebase Realtime download.

Firebase database structure

What I had tried:

VillazRef.child(**UID2**).addListenerForSingleValueEvent(new ValueEventListener() {
                                                    @Override
                                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                                                    {
                                                        if (dataSnapshot.child(VISIBLE).hasChild("OMBsclV...0X6w2"))
                                                        {
                                                            bool=true;
                                                        }
                                                        if (dataSnapshot.child(INVISIBLE).hasChild("OMBsclV...0X6w2"))
                                                        {
                                                            bool=true;
                                                        }
                                                    }
                                                    @Override
                                                    public void onCancelled(@NonNull DatabaseError databaseError) {
                                                    }
                                                });

I think these cades VillazRef.child(**UID2**).addListenerForSingleValueEvent return the whole datasnapshot of this location, but I don’t want this, its totally a waste of my bandwidth, so I need only like

if("OMBsclV...0X6w2" is within the VillazRef.child(**UID2**))
{
bool=true;
}
else
{
bool=false;
}

Answer

You’re almost there. All you need to do is to go a step deeper into your database hierarchy. Assuming that the following line of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

Will return “cxhcK … kj433” being the UID of the logged-in user (UID2), to check if “OMBsc … 0X6w2” is a child of the “visible” node, then please use the following lines of code:

String uidToSearch = "OMBsc ... 0X6w2";
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidToSearchRef = rootRef.child("Villaz")
                                          .child(uid)
                                          .child("visible")
                                          .child(uidToSearch);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()) {
            bool = true;
        } else {
            bool = false;
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
uidToSearchRef.addListenerForSingleValueEvent(valueEventListener);

In this way, you only download a single value from the database and not the entire “visible” node. However, since Firebase API is asynchronous, the value of “bool” cannot be simply used outside the “onDataChange()” method. To be able to use that value outside the callback, please check my answer from the following post:

Leave a Reply

Your email address will not be published. Required fields are marked *