Android Firebase - Denormalized Queries using FirebaseUI

寵の児 提交于 2019-12-08 02:43:42

问题


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.

来源:https://stackoverflow.com/questions/39333174/android-firebase-denormalized-queries-using-firebaseui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!