本文介绍了删除ChildEventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase childEventListener,将它添加到Node中N次,以便从数据库中获取数据.

I'm using Firebase childEventListener which is added N-number of times to a Node to get data from the DB.

现在,我正在尝试删除childEventListener,它似乎不起作用,并且我得到了重复的数据.

Now I'm trying to remove the childEventListener and it does not seem to work and I get duplicate data.

    query = FirebaseDatabase.getInstance().getReference()
            .child("chat")
            .child("room-messages")
            .child(roomID)
            .orderByChild("timestamp")
            .limitToLast(index);

    paginationListener = query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.d(TAG, dataSnapshot.getKey() + " - Single Value");
            try {
                Message message = dataSnapshot.getValue(Message.class);
                message.setId(dataSnapshot.getKey());
                EventBus.getDefault().post(new GetPaginatedMessage(message));
            } catch (RuntimeException e) {
                Log.d(TAG, "Type Cast Exception Caught");
            }

        }

并像这样删除监听器

 if (query != null && paginationListener != null) {
        query.removeEventListener(paginationListener);
    }

它似乎不起作用,并且我得到重复的数据.任何帮助都可以申请

It doesn't seem to work and I get duplicate data.Any help is appriciated

推荐答案

调用removeEventListener()会将侦听器从该位置删除.调用后,它将不会为接收到的任何数据触发事件.它可能仍会为已接收到的数据触发一些事件.

Calling removeEventListener() removes the listener from that location. It will not fire events for any data received after you call it. It may still fire some events for data it's already received.

Jonny对firebase-talk组的回答关于同样的行为:

这篇关于删除ChildEventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 00:49