0

In my Application I am using Firebase to retrieve the mobilnumbers of the Users. Therefore I use this code:

databaseUsers.orderByChild("uid").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            users.clear();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                User contactlists = postSnapshot.getValue(User.class);
                users.add(contactlists);
            }
            ContactList contactAdapter = new ContactList(ContactListActivity.this, users);
            listViewContacts.setAdapter(contactAdapter);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

Now I have the following question: If the number of users is high, is there a possibility to send only the mobilnumbers of your Phonebook maybe in a list? Otherwise I think the traffic to Firebase might be not so efficient?! The other opportunity would be to send each number individually but this might be quite complex if the user has many contacts.

At the moment I get all numbers from the server but I need to filter for the right contacts AND I need to display the names of the contact.

What is the best solution to use Firebase as efficient as possible and also get the names of the contacts?

Thank you in advance!

Timitrov
  • 211
  • 1
  • 3
  • 14

1 Answers1

3

You'll have to:

  1. Loop through the local phone book to find the phone number of each contact.
  2. Execute a query to Firebase for each number.
  3. Add the resulting contact (if any) to the list/adapter and update the view.

So say you've done step 1 and have a list of phone numbers. You'd then loop through those and for each:

for (String phonenumber: phonenumbers) {

    Query query = databaseUsers.orderByChild("phonenumber").equalTo(phonenumber);

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User contactlists = postSnapshot.getValue(User.class);
            users.add(contactlists);
            adapter.notifyDataSetChanged();
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException(); // don't ignore erors
        }
    });
}

The call to notifyDataSetChanged() ensures that the adapter know that it needs to update the view.

While the code gets a bit convoluted, it is not as slow as you may initially fear, since Firebase pipelines the requests over a single connection. The performance will mostly depend on the number of users you have in the database, but up to a few hundreds of thousands this should be fine.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    here when mobile number is different like in firebase save with +91 and in contact without +91 that time what i do? – user7176550 Nov 23 '17 at 06:06