Firebase Android ChildEventListener 在 onChildAdded() 添加新数据时触发 onChildRemoved()

Mun*_*oor 4 android firebase firebase-realtime-database

我将新数据添加到 Firebase,如下代码所示

updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError,   DatabaseReference databaseReference) {

        }
}
Run Code Online (Sandbox Code Playgroud)

以及 ChildEventListener 的以下代码

fb.child(Organizations.class.getSimpleName().toLowerCase()).limitToLast(1).addChildEventListener(crudListener);

ChildEventListener crudListener = new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.e(TAG,"onChildAdded "+ dataSnapshot.toString()+" \n String "+s);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Log.e(TAG,"onChildChanged "+ dataSnapshot.toString()+" \n String "+s);
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            Log.e(TAG,"onChildRemoved "+ dataSnapshot.toString());
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            Log.e(TAG,"onChildAdded "+ dataSnapshot.toString()+" \n String "+s);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

};
Run Code Online (Sandbox Code Playgroud)

问题是当我创建新数据时onChildRemoved()在onChildAdded() 之前触发

我没有删除任何孩子,但每次创建新孩子时都会触发onChildRemoved()

遵循 Web(javascript) 代码,它工作正常

 firebase.initializeApp(config);


 var commentsRef = firebase.database().ref('organizations').limitToLast(1);
 commentsRef.on('child_added', function(snap) {
    $('pre').text("child_added: "+JSON.stringify(snap.val(), null, 2));
 });

 commentsRef.on('child_changed', function(snap) {
    $('pre').text("child_changed: "+JSON.stringify(snap.val(), null, 2));
 });

 commentsRef.on('child_removed', function(snap) {
    $('pre').text("child_removed: "+JSON.stringify(snap.val(), null, 2));
 });
Run Code Online (Sandbox Code Playgroud)

这是我的数据库结构。我正在组织下创建新孩子

在此输入图像描述

请帮助我如何解决这个问题

Wil*_*lik 5

li\xe2\x80\x8cmitToLast(1)您提到您在附加时使用crudListener. 这实际上就是它的工作原理。

\n\n

来自Quer#limitToLast() 参考

\n\n
\n

limitToLast()方法用于设置给定回调要同步的子级的最大数量。如果我们将限制设置为 100,那么我们最初最多只能接收 100 个child_added事件。如果数据库中存储的消息少于 100 条,则child_added每条消息都会触发一个事件。但是,如果我们有超过 100 条消息,我们将仅收到最后 100 条有序消息的 child_added 事件。当项目发生变化时,我们将收到child_removed从活动列表中删除的每个项目的事件,以便总数保持在 100。

\n
\n\n

如果使用li\xe2\x80\x8c\xe2\x80\x8bmitToLast(1),则onChildRemoved()在插入新子项以维持限制(即 1)时将调用 ,然后onChildAdded()调用新子项。

\n\n

如果您不希望出现此行为,请删除该li\xe2\x80\x8c\xe2\x80\x8bmitToLast(1)方法

\n\n

编辑: \n如果您想继续使用limitToLast(1),那么您必须检查组织条目是否确实从数据库中删除,或者是否来自限制行为。

\n\n
@Override\npublic void onChildRemoved(DataSnapshot dataSnapshot) {\n    dataSnapshot.getRef().addListenerForSingleValueEvent(new ValueEventListener() {\n        @Override\n        public void onDataChange(DataSnapshot dataSnapshot) {\n            if (!dataSnapshot.exists()) {\n                // remove this organization item from the RecyclerView\n            }\n        }\n\n        @Override\n        public void onCancelled(DatabaseError databaseError) {\n\n        }\n    });\n}\n
Run Code Online (Sandbox Code Playgroud)\n