gpt4 book ai didi

android - 添加新数据时,Firebase Android ChildEventListener 在 onChildAdded() 之前触发 onChildRemoved()

转载 作者:搜寻专家 更新时间:2023-11-01 07:48:03 24 4
gpt4 key购买 nike

我正在按照以下代码向 Firebase 添加新数据

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

}
}

然后是 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) {

}

};

问题是当我创建新数据时 onChildRemoved()onChildAdded()

之前触发

我没有删除任何 child ,但每次创建新 child 时都会触发 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));
});

这是我的数据库结构。我正在组织

下创建新的 child

enter image description here

请帮我解决这个问题

最佳答案

您提到在附加 crudListener 时您正在使用 li‌mitToLast(1)。这实际上是它的工作原理。

来自 Quer#limitToLast() reference

The limitToLast() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have less than 100 messages stored in our database, a child_added event will fire for each message. However, if we have over 100 messages, we will only receive a child_added event for the last 100 ordered messages. As items change, we will receive child_removed events for each item that drops out of the active list, so that the total number stays at 100.

如果您使用 li‌ mitToLast(1),则在插入新子项以维持限制(即 1)时将调用 onChildRemoved(),然后 onChildAdded() 调用了新的 child 。

如果您不想要这种行为,请删除 li‌ mitToLast(1) 方法

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

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
// remove this organization item from the RecyclerView
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

关于android - 添加新数据时,Firebase Android ChildEventListener 在 onChildAdded() 之前触发 onChildRemoved(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com