I understand from the Structuring Data section in the documentation that denormalizing data is useful to achieve more efficient look ups when you have two way relationships.
In my app, I have a denormalized data structure for chats
and users
:
{
users: {
user1: {
name: "user1",
chats: {
chat1: true,
chat3: true,
}
}
user2: {
name: "user2",
chats: {
chat2: true,
chat3: true,
}
}
chats: {
chat1: {
chatID: "chat1",
chats: {
user1: true,
}
}
chat3: {
chatID: "chat3" ,
users: {
user1: true,
user2: true,
}
}
}
}
I want to query the database for all chats that a particular user is a part of, and load those into a RecyclerView
.
Right now, I have a working solution using the FirebaseUI RecyclerViewAdapter
in which I set up the FirebaseRecyclerView
adapter to query the users
/userID
/chats
reference, and then I set a ValueEventsListener
on each chat with key that the adapter finds:
FirebaseRecyclerAdapter<Boolean, ChatViewHolder> adapter = new
FirebaseRecyclerAdapter<Boolean, ChatViewHolder>(
Boolean.class,
R.layout.chat_list_item,
ChatViewHolder.class,
mUserRef.child(mUserID).child("chats")){
@Override
protected void populateViewHolder(final ChatViewHolder viewHolder, Boolean model, final int position) {
final String key = this.getRef(position).getKey();
mChatRef.child(key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Chat chat = dataSnapshot.getValue(Chat.class);
viewHolder.bindTo(chat);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent chatIntent = new Intent(ChatActivity.this, MessageActivity.class);
chatIntent.putExtra("chatID", key);
startActivity(chatIntent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
This works, but I am afraid of the performance implications since I am adding a new listener to each item independently, rather than a ChildEventListener
on a subset of chat
items.
Is this the best way to handle this? Thanks in advance for your help.