【发布时间】:2017-05-12 10:32:15
【问题描述】:
我在收听数据时有一个奇怪的结果。
对于节点“allstudents”上的 3 个孩子,onchildadded 方法为第 0 个元素返回 null,但是,将第 0 个 pushid 分配给第一个元素,依此类推。
当我调整时,ID(-Kjw7V4jSphLBYs7Z_wx) onChildChanged 被触发为 null 但是,调整 FB 控制台上的 ID(-Kjw7bRY7AkJnGpOGJEw) onChildChange 返回不同的 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
规则:
"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()方法中使用字符串s? -
谢谢@AlexMamo,我使用
dataSnapshot获得了有效数据,但是Strings参数的意义是什么? -
请看我的回答和
String s的解释。
标签: java android firebase firebase-realtime-database firebase-security