How to use Snapshot listener to get real time update in a recyclerview?

本小妞迷上赌 提交于 2020-02-27 03:50:06

问题


Below is the code I used to retrieve documents data in a recyclerview. It works fine. But whenever the new document is added, it does not update it in real time. I know snapshot listener is for that purpose only but having a hard time to get it work. Any help will be appreciated. :)

 mFirestore.collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()){
                    for (DocumentSnapshot document : task.getResult()) {
                        Message message = document.toObject(Message.class);
                        messageList.add(message);
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

回答1:


you should separate the snapshot event like this.. then you can easily find out, what's the problem

mFirestore.collection("Users")
        .addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot snapshots,
                                @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.w("TAG", "listen:error", e);
                    return;
                }

                for (DocumentChange dc : snapshots.getDocumentChanges()) {
                    switch (dc.getType()) {
                        case ADDED:
                            Log.d("TAG", "New Msg: " + dc.getDocument().toObject(Message.class));
                            break;
                        case MODIFIED:
                            Log.d("TAG", "Modified Msg: " + dc.getDocument().toObject(Message.class));
                            break;
                        case REMOVED:
                            Log.d("TAG", "Removed Msg: " + dc.getDocument().toObject(Message.class));
                            break;
                    }
                }

            }
        });

Maybe the snapshot you got, was triggered by [MODIFIED] event, not [ADDED]..




回答2:


If you want to keep listening your Firestore data (realtime update), you should do like this:

mFirestore.collection("Users")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
                                    @Nullable FirebaseFirestoreException e) {

                    if (e != null) {
                        Log.w("YourTag", "Listen failed.", e);
                        return;
                    }

                    for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                        if (doc.exists()){
                            Message message = doc.toObject(Message.class);
                            messageList.add(message);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                    Log.d("YourTag", "messageList: " + messageList);
                }
            });

the way you use only retrieve Firestore data one time.

Check this >> https://firebase.google.com/docs/firestore/query-data/listen



来源:https://stackoverflow.com/questions/51335853/how-to-use-snapshot-listener-to-get-real-time-update-in-a-recyclerview

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