1

How can i get All chats where Users contains a specific Uid

 Query sItemQuery = mDatabase.getReference().child("chats").limitToLast(200);

        FirebaseRecyclerOptions<ChatMetaData> options =
                new FirebaseRecyclerOptions.Builder<ChatMetaData>()
                        .setQuery(sItemQuery, ChatMetaData.class)
                        .setLifecycleOwner(this)
                        .build();

This is my DB Structure

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

2

You can't query across multiple levels like that. See Firebase Query Double Nested

So while your current data structure makes it easy to find the current users in a chat, it doesn't make it easy to find the current chats for a user.

For that you'll need to add an additional data structure. Something like:

user_chats: {
  uid1: {
    chatid1: true,
    chatid2: true
  }
  uid2: {
    chatid2: true,
    chatid3: true
  }
}

With this additional structure, you can easily find the chats for the current user by reading /user_chats/$uid.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807