Firebase实时数据库加载数据缓慢

时间:2019-07-18 03:59:07

标签: android firebase-realtime-database

作为标题,至少需要10秒钟才能隐藏Progressbar并显示RecyclerView。 10秒不是等待用户的可接受时间。我想使其更快。如果需要,我将更改数据库结构。我正在使用4G加载数据,因此互联网将足够快。

这是我的数据库结构

Message
---UserId_1
------UserId_2
---------MessageId_1
------------Message_1
---------MessageId_2
------------Message_2

Users
---UserId_1
------UserProfile_1
---UserId_2
------UserProfile_2

这是我的代码,用于从数据库加载对话。我第一次进行查询,以了解数据库上是否没有对话。如果它至少有1个对话,则我将隐藏Progressbar并显示RecyclerView,否则我将为用户显示“无对话”布局



        mConversationsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                if (dataSnapshot.getChildrenCount() == 0) {

                    linear_no_conv.setVisibility(View.VISIBLE);

                    rv_conversations.setVisibility(View.GONE);
                    linear_loading.setVisibility(View.GONE);

                } else {

                    linear_loading.setVisibility(View.VISIBLE);

                    linear_no_conv.setVisibility(View.GONE);
                    rv_conversations.setVisibility(View.GONE);

                }

                loadConversations();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

OnChildAdded在加载对话方法中:

        ROOT_REF.child(CHILD_MESSAGES)
                .child(getMyUID()).child(conversationId)
                .limitToLast(1).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    Object_Message message = snapshot.getValue(Object_Message.class);


                        ROOT_REF.child(CHILD_USERS)
                                .orderByKey().equalTo(conversationId)
                                .addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                                            Object_User user = snapshot.getValue(Object_User.class);

                                            Object_Conversation conversation = new Object_Conversation(conversationId, user, message);

                                            mConversationsList.add(conversation);
                                            mConversationsAdapter.notifyItemInserted(mConversationsList.size() - 1);

                                            mActivity.updateConversationsBadges(countUnSeenMessages());

                                            linear_no_conv.setVisibility(View.GONE);
                                            linear_loading.setVisibility(View.GONE);
                                            rv_conversations.setVisibility(View.VISIBLE);

                                            sortList();


                                        }
                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {

                                    }
                                });
                    }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

0 个答案:

没有答案