我在收聽數據時有奇怪的結果。Firebase爲什麼onChildAdded參數s返回null
對於節點'allstudents'上的3個子節點,onChildadded方法爲0個元素返回null,但是將0個pushid指派給1st元素,依此類推。
當我調整,ID(-Kjw7V4jSphLBYs7Z_wx
)onChildChanged觸發與空但是,FB-控制檯上調整ID(-Kjw7bRY7AkJnGpOGJEw
)onChildChange返回differt ID(-Kjw7YchGZLamQmWTTc5
)爲什麼?
logcat的
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);
retriving /收聽數據
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()'方法中使用String's'? –
謝謝@ AlexMamo,我使用'dataSnapshot'獲得有效的數據,但是,String's'參數的意義是什麼? – user755
請參閱我的回答以及'String s'的解釋。 –