AmirM86
AmirM86

Reputation: 113

Firebase Database Android

I have been working with google FireBase for some time, and I noticed that there is no way to fetch a bulk of data according to any input.

I will explain with an example: Let's say I have an ArrayList which contains all of my friends cell numbers. If I want to check if each one of my friends are exist in the db I need to do a loop on the array and check every each one separately which I think is not that good.

So the question is there any option to send the ArrayList as an input and return only the existed ones at once? The main idea is not to connect to the firebase as the size of the arraylist.

I tried to google that out, but still couldn't find any good solution for now. If anyone has a better idea also will be appreciated.

Upvotes: 1

Views: 70

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 139019

A better way to achieve this is to use create a new node in your Firebase database named phoneNumbers, than create a listener and than use exists() method. You database should look like this:

Firebase-root
   |
   ---- phoneNumbers
       |
       ---- +1-111-111-1111: true
       |
       ---- +1-111-111-1112: true
       |
       ---- +1-111-111-1112: true

And here is to code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference phoneNumbersRef = rootRef.child("phoneNumbers");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.child(phoneNumber).exists()) {
            Log.d("TAG", "Number exists!");
        } else {
            Log.d("TAG", "Number does not exist!");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
phoneNumbersRef.addListenerForSingleValueEvent(eventListener);

Upvotes: 1

Related Questions