gpt4 book ai didi

android - onChildAdded 回调被调用但没有添加

转载 作者:行者123 更新时间:2023-11-29 00:04:39 25 4
gpt4 key购买 nike

我有一个问题,就是 onChildAdded() 被无缘无故地调用,并且当添加某些东西时它确实工作正常

这是监听器:

    comment = new Firebase(MY_URL).child("Comments").child(channel);
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
try {
comment = new ObjectMapper().writeValueAsString(commentObject);
System.out.println("onChildAdded"+comment +" s"+s);
} catch (JSONException e) {
e.printStackTrace();
}catch (JsonProcessingException e) {
e.printStackTrace();
}
}

}
//the other call backs
};
comment.addChildEventListener(childEventListener);

这是我添加监听器的初始化代码:

@Override
public void onResume() {
super.onResume();
setupFireBaseListener(channel);
}

这是我删除监听器的方式:

 @Override
public void onDetach() {
super.onDetach();
comment.removeEventListener(childEventListener);
}

请注意,我在 Application 类上调用了此方法:

Firebase.setAndroidContext(this); 

这是我打印的数据:

onChildAdded{"name":"osama raddad","comment":"hi","user_id":"11"}    s-K7qX32GeNLEvY4IDtnn oldData
onChildAdded{"name":"osama raddad","comment":"how","user_id":"11"} s-K7qX3UCI6vlcS5kfBdG oldData

我的firebase客户端sdk版本是2.2.1

最佳答案

onChildAdded 不仅会在您连接后为每个添加的 child 触发,还会为每个现有 child 触发。

以下来自 Firebase 文档。

...the onChildAdded event is triggered once for each existing child and then again every time a new child is added to the specified path.

如果您只需要新添加的项目,则需要在每个项目中包含时间戳,然后执行 orderByChild("timestamp").start(currentTimestamp) 查询。

关于android - onChildAdded 回调被调用但没有添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761504/

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