1

I'm developing an Android messenger app. To do so, users sign up with phone number that are stored on Firebase Realtime Database.

To start texting someone, user should have, in his contacts, other signed up users on the DB.

To do that I have a method to retrieve contacts from user's phone and signedUp users on the DB.

private List<String> getContactsFromFirebase(final MyCallback myCallback){
    FirebaseDatabase.getInstance().getReference().child("Users")
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        Users user = snapshot.getValue(Users.class);
                        assert user != null;
                        String contact_found = user.getPhone_number();
                        mContactsFromFirebase.add(contact_found);
                    }
                    myCallback.onCallback(mContactsFromFirebase);
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
    return mContactsFromFirebase;

}

And compared users found on DB with user's contacts

private List<String> findSignedUpContacts(Map<String, String> contactsStored, List<String> firebaseNumbers) {

    List<String> values = new ArrayList<>();
    for (String firebaseNumberKey : firebaseNumbers) {
        values.add(contactsStored.get(firebaseNumberKey));
    }
    return values;
}

Now, I want to retrieve from Firebase DB, only information regarding numbers stored in the list created above(numbers that match across DB & contacts) and display the result in a FirebaseRecyclerAdapter.

How can I please do that?

Here is my DB structure.

Users
    5AWIQqYkdoZwFBqM0YrvZiqBj633
        image: "https://firebasestorage.googleapis.com/v0/b/cha..."
        name:  "Plasita"
        phone_number: "+50940404200"
        status: "Coding is my life.."
        thumbnail: "Default"
Panther007
  • 255
  • 3
  • 9
  • Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo May 27 '18 at 07:04

1 Answers1

2

Sorry, there is no machanism in Firebase to query by a list, you'd need to run through the list and query each element, which is very inefficient. What I recommend is: as you're already bringing all elements from the "Users" node, store them in a secondary data structure and filter that.

 for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
      Users user = snapshot.getValue(Users.class);
      assert user != null;
      String contact_found = user.getPhone_number();
      mContactsFromFirebase.add(user);//this data structure now takes users

  }



private List<User> findSignedUpContacts(Map<String, String> contactsStored, List<User> firebaseUsers) {

    List<User> values = new ArrayList<>();
    for (User user : firebaseUsers) {
        if(contactsStored.containsKey(user.getPhone_number()) 
        {
            values.add(user);
        }
    }
    return values;
}

Now values have the data about the users that are matched with the ones in your phone.

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46