0

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;
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Vora Ravi
  • 5
  • 5

1 Answers1

0

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:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • suppose if(!dataSnapshot.exists()) than bool = false; so this will be calculated in download or not ? i am asking this because this lisetner(answered by you) will work in for loop up to million times, so cost is first priority – Vora Ravi Jan 13 '21 at 19:26
  • but why i will get 1 download even if(!dataSnapshot.exists()) ,, so i got nothing from database than why it calculate in cost also ? – Vora Ravi Jan 13 '21 at 20:31
  • I'll rephrase. `if(!dataSnapshot.exists()) than bool = false;` yes, that's correct, **otherwise** it will be calculated only for one download. My answer will not loop up to a million times, if the data exists, it will always get one item. It doesn't matter how many elements exist in the "visible" node, you'll always get the "uidToSearch", if it exists. – Alex Mamo Jan 14 '21 at 00:37