0

I'm using firebase with android to create a simple chat app. When the user chooses another user to chat with I want to check whether they've chatted together or not.

In onCreate() method I'm retrieving all the rooms that the current user used before, and I'm putting them in an arraylist called MyChatRooms<>. Then I want to check each room to see the users of the room.

The problem is that the loop I'm using to iterate through rooms name is finishing before I'm able to retrieve any data from the database.

I know there's similar questions to mine, but none of the answers worked for me.

Here's the related code:

if (!MYChatRooms.isEmpty()) {
  for (j = 0; j < MYChatRooms.size(); j++) {
    roomref.child(MYChatRooms.get(j)).child("First User").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot1) {
        if (!dataSnapshot1.getValue().toString().equals(Username) && dataSnapshot1.getValue().toString().equals(NUsername)) {
          Users += dataSnapshot1.getValue().toString() + ",,, ";
        } else if (dataSnapshot1.getValue().toString().equals(Username)) {
          roomref.child(MYChatRooms.get(j)).child("Second User").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot2) {
              if (dataSnapshot2.getValue().toString().equals(NUsername)) {
                Users += dataSnapshot2.getValue().toString() + ",,, ";
              }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
          });
        }
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
   });
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
amm965
  • 399
  • 5
  • 14
  • The code is hard to read without seeing the corresponding data structure. Please edit your question to include a snippet of the JSON (as text, no screenshots). You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 27 '18 at 13:17
  • 1
    But my first guess is that you should consider using a different data structure. Looping over room to find a matching one can be pretty expensive. The answer from M.Waqas Pervez shows one alternative structure, but also consider using a room name/key that identifies the users, as I've shown here: http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Jun 27 '18 at 13:19

3 Answers3

1

I would suggest that you change the structure of your data. Imagine if a user has 100 chats that means your have to query 200 times to Firebase that of course does not look feasible.

What i would suggest is that your add a recentChat list in every user and whenever a user starts a new chat with someone you add the id of the second user to that list. That way you can track easily with whom the current user has interacted with.

It structure in firebase can look something like this:

  • User
    • recentChats
      • id of the other user
M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
1

Try to change you database hierarchy or use firestore instead of real time database

Yahya
  • 210
  • 2
  • 12
0

Please check the following topic: https://firebase.google.com/docs/database/usage/optimize? In my case, I had added and index column and limited the query in Firebase Rules.

user49068
  • 11
  • 3