1

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.

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply man . But this I already have in my DB . the question is a little bit different . Assume I have in the DB 1000 numbers and in my phone I have 10 people , 5 of them has the app and 5 doesn't. In general you would loop on the 10 people you have and check if they are exists in the DB , but looping on 1000 is not good . I want to send an array of 10 to the db at once and to return only 5 , in order to have a fast run .
It will work for sure even if you'll loop trough 1000 memebers. Because there is no way in the Firebase Database to download just one property of each node, the single way to achieve this is to use that new node as explained above. So if you want an efficient way to verify the existens of a phone number, download just the list of phoneNumbers. For good measure you should keep precisely that list of phone numbers in the database .
Well thanks again , I have same example you mentioned above , but I think firebase is a little bit strict since the db does not work as SQL .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.