gpt4 book ai didi

java - Firebase为什么onChildAdded参数s返回null

转载 作者:行者123 更新时间:2023-12-02 13:11:42 25 4
gpt4 key购买 nike

我在监听数据时得到了奇怪的结果。

对于节点“allstudents”上的 3 个子节点,onchildadded 方法会为第 0 个元素返回 null,但会将第 0 个 Pushid 分配给第一个元素,依此类推。

当我调整时,ID(-Kjw7V4jSphLBYs7Z_wx) onChildChanged 被 null 触发,但是,在 FB 控制台 onChildChange 上调整 ID(-Kjw7bRY7AkJnGpOGJEw) 返回不同的 ID(-Kjw7YchGZLamQmWTTc5) 为什么?

日志猫

 E/String: onChildAdded:null
E/String: onChildAdded:-Kjw7V4jSphLBYs7Z_wx
E/String: onChildAdded:-Kjw7YchGZLamQmWTTc5
E/String: onChildChanged:-Kjw7YchGZLamQmWTTc5
E/String: onChildChanged:-Kjw7V4jSphLBYs7Z_wx
E/String: onChildChanged:null

enter image description here

规则:

"teachers": {
"$teacherID": {
".read": "auth != null && auth.uid == $teacherID",
".write": "auth != null && auth.uid == $teacherID",
".validate":"root.child('teacherids').child(newData.child('tID').val()).exists()"
}
},
// teachers can r/w student profiles, and the students can also r/w their own profile
"students": {
"$studentID": {
".read": "auth != null && (root.child('teachers').child(auth.uid).exists() || auth.uid == $studentID)",
".write": "auth != null && (root.child('teachers').child(auth.uid).exists() || auth.uid == $studentID)",
".validate":"root.child('studentids').child(newData.child('rollnr').val()).exists()"
}
},

"allstudents":{
".read": "auth != null && (root.child('teachers').child(auth.uid).exists() || root.child('students').child(auth.uid).exists() )",
".write": "auth != null && (root.child('teachers').child(auth.uid).exists() || root.child('students').child(auth.uid).exists() )"

}

向节点添加数据

mDatabase.child("allstudents").push().setValue(allstudentnode);

检索/监听数据

mDatabase.child("allstudents").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.e(TAG, "onChildAdded:"+s);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.e(TAG, "onChildChanged:"+s);
}

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

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

@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled:"+databaseError.toString());
}
});

最佳答案

要在代码中使用有效数据,您需要从 dataSnapshot 对象中获取数据,而不是使用 onChildAdded( ) 方法。

正如您在 official doc 中看到的那样,String s 实际上代表 String previousChildName,这就是您在代码中拥有前一个子项的方式。

希望有帮助。

关于java - Firebase为什么onChildAdded参数s返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43935578/

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