11

In apps that use phone authentication like whatsapp:

When any user opens to check his/her contacts, whatsapp will show only the contacts from your phone that are using whatsapp (like they filter your contacts).

My thoughts on implementing something similar:

I can save all users in a database (when they authenticate with my app), now when I load contacts in an activity I check to see if the contact exist in the database, and if the user exist then I add him/her to the list.

Problem:

If I used the above method:

Then lets say I have 10,000 users in the database, then it wont make sense to loop through 10,000 users every time I want to show contacts of a user inside my app.

Question:

How do you think apps like whatsapp implement such a thing?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
data
  • 739
  • 6
  • 17
  • 1
    The search you are asking about probably uses an index in the database to significantly increase the speed. It doesn't do a linear search for each user as that would take too much time, as you say. After you look up a user the first time, you can just store it in your own database. – Code-Apprentice Aug 11 '18 at 15:25
  • @Code-Apprentice if I store them locally after a single read then how to handle if a contact in my list deleted his/her account? – data Aug 11 '18 at 16:08
  • You can periodically iterate through the users in the local database to check if they were deleted/deactivated. – Code-Apprentice Aug 11 '18 at 16:29
  • Check out my answer here : https://stackoverflow.com/a/64399981/13124119 – Kamal Panara Oct 17 '20 at 07:41
  • Does this answer your question? [How do I display list of only registered contacts(on firebase) from a client's address book(via phone numbers)](https://stackoverflow.com/questions/45175845/how-do-i-display-list-of-only-registered-contactson-firebase-from-a-clients-a) – Scratte Oct 17 '20 at 08:40

1 Answers1

9

You have to loop through your users with contacts at least one time but you can do it smartly just like WhatsApp do.

When user first time open and register in WhatsApp they ask for your permission to read your contacts (From API Level 23) , when user grant the permission WhatsApp in background connect to their backend server (where they save their all user's data) with the contacts available in user's device and in response of this http request only those numbers returns with their account info which are registered on WhatsApp and then WhatsApp locally they save them user's device (SQLite or any database related service) and when user open WhatsApp's contacts activity it shows data from the local user database not the server.

Now question is : What happen when new Contact is Added?

WhatsApp will be notified if user created new contact and then WhatsApp again connect to their backend server with the new contact's number. Now suppose if user was not connected to the internet when creating the new contact then what happen?

WhatsApp will save the number to another table of their local database as not checked number and then once again when user will connect to internet whatsapp will again connect to their backend server with same type of request. Also in WhatApp's contacts activity there is a option called force refresh which do the same thing but not in background.

this is same for deleting a contact except there is no need to connect to server it actually deletes the WhatsApp contact from user local database.

Now question is what are the benefits of this?

  • Small data uses which is great for user experience
  • No need to contact to server every time user open the contacts activity. Which is also great for user experience.
Sayan Roy
  • 134
  • 5
  • Runtime permission are added in API level 23 not 21. – Mohammed Farhan Aug 11 '18 at 16:00
  • Now you mean that I must check if users exist in the database only once and save them locally......another question comes up to my mind: What happens if a user is deleted from the database (in other words a user decides to delete his/her account and he/she for some reason was my contact....how my local list should refresh?) – data Aug 11 '18 at 16:04
  • In some interval of time everyday you must check user's contact list just like when user first time open the app and then in response if a user is not in the list of server response but in user's contact list of your app then delete the account from your app's contact list database and also do the same thing when user force refresh the contact in contact activity. – Sayan Roy Aug 11 '18 at 16:45