Firebase Android ChildEventListener在onChildAdded()之前触发onChildRemoved()添加新数据时 [英] Firebase Android ChildEventListener Triggered onChildRemoved() before onChildAdded() When new data added
问题描述
我正在向Firebase添加新数据,如下所示:
updateChildren(childUpdates,new DatabaseReference.CompletionListener(){
@Override
public void onComplete(DatabaseError databaseError,DatabaseReference databaseReference){
$ b}
}
以下代码为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()+\\\
String+ s);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
Log.e(TAG,onChildChanged+ dataSnapshot.toString()+\\\
S tring+ s);
$ b @Override
public void onChildRemoved(DataSnapshot dataSnapshot){
Log.e(TAG,onChildRemoved+ dataSnapshot.toString());
$ b @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){
Log.e(TAG,onChildAdded+ dataSnapshot.toString()+ \\\
String+ s);
$ b @Override
public void onCancelled(DatabaseError databaseError){
};
问题是当我创建新的数据,然后 onChildRemoved() onChildAdded()
我不会删除任何小孩,但每次创建新子项时都会触发 onChildRemoved()
以下Web(javascript)代码正常工作
firebase。 initializeApp(配置);
var commentsRef = firebase.database()。ref('organizations')。limitToLast(1);
commentRef.on('child_added',function(snap){
$('pre')。text(child_added:+ JSON.stringify(snap.val(),null,2)) ;
});
$ b commentRef.on('child_changed',function(snap){
$('pre')。text(child_changed:+ JSON.stringify(snap.val(),null ,2));
});
$ b commentRef.on('child_removed',function(snap){
$('pre')。text(child_removed:+ JSON.stringify(snap.val(),null ,2));
});
这里是我的数据库结构。我正在组织下创建新的孩子
Pleaes帮我解决这个问题
你提到你正在使用 limitToLast(1)
附加 crudListener
时。这实际上是如何工作的。
来自 Quer#limitToLast()引用
$ b
limitToLast()
方法用于设置给定回调同步的最大子数。如果我们设置了100的限制,我们最初只会收到最多100child_added
事件。如果我们的数据库中存储的消息少于100条,则每条消息都会触发child_added
事件。但是,如果我们有超过100条消息,则只会收到最后100条订购消息的child_added事件。随着项目的变化,我们将收到每个退出活动列表的项目的child_removed
事件,以便总数保持在100。
如果您使用 如果您不想要这种行为,请移除 编辑: I am adding new data to Firebase as following code And following code for ChildEventListener issue is when I am creating new data then onChildRemoved() triggered before onChildAdded() I am not removing any child but onChildRemoved() triggered every time when new child is created following Web(javascript) code it's working fine here is my Database structure. I am creating new child under organizations Pleaes help me how to fix this issue You mentioned you're using from Quer#limitToLast() reference The If you use If you don't want this behavior, remove the EDIT:
If you want to keep using
这篇关于Firebase Android ChildEventListener在onChildAdded()之前触发onChildRemoved()添加新数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! li mitToLast(1)
, onChildRemoved() code>将被调用,如果插入一个新的孩子来维持限制(这是1),然后
onChildAdded()
调用新的孩子。 >
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()){
//从RecyclerView
中删除这个组织项目}
}
@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});
}
updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
}
}
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) {
}
};
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));
});
limitToLast(1)
when attaching crudListener
. That's actually how it works.
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.limitToLast(1)
, the onChildRemoved()
will be called if a new child is inserted to maintain the limit (which is 1), and then onChildAdded()
called the new child.limitToLast(1)
methodlimitToLast(1)
, then you have to do checking if an organization entry is really removed from the database or it's from the limit behavior.@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) {
}
});
}