本文介绍了如何在 RecyclerView Item 的 populateViewHolder 中设置 addSnapshotListener 和删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 FirebaseUI-Android 库实现了 RecyclerView.

一旦我使用了 FirebaseRecyclerAdapter

My RecyclerView realtime data change well once i use that FirebaseRecyclerAdapter

在集合中,数据文档的字段类型为 Boolean , Integer, Reference.

In Collection that data document have field type as Boolean , Integer, Reference.

希望使用该引用通过 addSnapshotListener 获取 populateViewHolder 中的数据.

帮帮我!这是我的代码:

Help me! Here is my code:

         FirebaseRecyclerAdapter<Conv, ConvViewHolder> firebaseConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(
            Conv.class,
            R.layout.users_single_layout,
            ConvViewHolder.class,
            conversationQuery
    ) {
        @Override
        protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int i) {

            final String list_user_id = getRef(i).getKey();

            final DocumentReference docRef = db.collection("cities").document(list_user_id);
            docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
                @Override
                public void onEvent(@Nullable DocumentSnapshot snapshot,
                                    @Nullable FirebaseFirestoreException e) {
                    if (e != null) {
                        Log.w(TAG, "Listen failed.", e);
                        return;
                    }

                    if (snapshot != null && snapshot.exists()) {
                        Log.d(TAG, "Current data: " + snapshot.getData());
                    } else {
                        Log.d(TAG, "Current data: null");
                    }
                }
            });
        }
    };

    mConvList.setAdapter(firebaseConvAdapter);

Firebase 说如果你添加 addSnapshotListener 然后必须删除它一次不需要 分离监听器

Firebase saying if you add addSnapshotListener then must to remove it once no need for that Detach a listener

当您不再对侦听数据感兴趣时,您必须分离侦听器,以便停止调用事件回调.这允许客户端停止使用带宽来接收更新.您可以使用 onSnapshot() 上的取消订阅功能来停止监听更新.

推荐答案

为了实现这一点,你需要使用一个 EventListener 像这样:

To achieve this, you need to use a EventListener<DocumentSnapshot> like this:

EventListener<DocumentSnapshot> eventListener = new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
        if (snapshot != null && snapshot.exists()) {
            //Do what you need to do
        }
    }
};

声明一个全局的 ListenerRegistration listenerRegistration; 变量并在需要的地方添加 SnapshotListener,如下所示:

Declare a global ListenerRegistration listenerRegistration; variable and add the SnapshotListener in the place where is needed like this:

if (listenerRegistration == null ) {
    listenerRegistration = yourRef.addSnapshotListener(eventListener);
}

要删除侦听器,只需在您的 onStop() 方法中使用以下代码行:

To remove the listener, just use the following lines of code, in your onStop() method:

@Override
protected void onStop() {
    if (listenerRegistration != null) {
        listenerRegistration.remove();
    }
}

此外,不要忘记在调用 onStart() 方法后再次添加它.

Also, don't forget to add it again once your onStart() method is called.

@Override
protected void onStart() {
    super.onStart();
    listenerRegistration = yourRef.addSnapshotListener(eventListener);
}

当您调用 addSnapshotListener 进行收听时到实时更新,这意味着您附加了一个侦听器,该侦听器会为数据库中发生的每个更改而调用.因此,当您的应用程序关闭时也会发生这种情况,这就是为什么必须 分离活动被销毁之前的侦听器.

When you are calling addSnapshotListener for listening to realtime updates, it means that you attach a listener that gets called for every change that takes place in your database. So this is happening also when your app is closed, that's why it's mandatory to detach the listeners before the activity gets destroyed.

如果在您的应用中您不需要实时获取数据,那么您只需使用 get() 直接调用引用,它只读取文档一次.由于它只读取一次,因此没有要删除的侦听器.这是 Firebase 实时数据库中使用的 addListenerForSingleValueEvent() 的对应者.

If in your app you don't need to get the data in realtime, then you can simply use a get() call directly on the reference, which just reads the document only once. Since it only reads once, there is no listener to be removed. This is the correspondent of addListenerForSingleValueEvent() used in the Firebase realtime database.

还有一种更优雅的方法来删除侦听器,即首先将活动作为参数传递给 addSnapshotListener() 方法,因此 Firestore 可以在 Activity 停止时自动清理侦听器.

There is also a more elegant way for removing the listener which is to pass the activity as first the argument in the addSnapshotListener() method, so Firestore can clean up the listeners automatically when the activity is stopped.

ListenerRegistration lg = yourDocumentRef
            .addSnapshotListener(YourActivity.this, eventListener);

这篇关于如何在 RecyclerView Item 的 populateViewHolder 中设置 addSnapshotListener 和删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 15:34
查看更多