Firebase 为什么 onChildAdded 参数 s returns null

Firebase why onChildAdded parameter s returns null

我在监听数据时有一个奇怪的结果。

对于节点 'allstudents' 上的 3 children,onchildadded 方法 returns null 但是,对于第 0 个元素,将第 0 个 pushid 分配给第一个元素,依此类推。

当我调整时,ID(-Kjw7V4jSphLBYs7Z_wx) onChildChanged 被 null 触发但是,在 FB-console onChildChange return 上调整 ID(-Kjw7bRY7AkJnGpOGJEw) 不同 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/listening数据

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());
     }
 });

试试这个

mDatabase= FirebaseDatabase.getInstance();
mDatabase.getReference("allstudents").addValueEventListener(new 
ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
              for(DataSnapshot dst : dataSnapshot.getChildren()){

                String key = dst.getKey();
                Log.e(TAG, "onChildAdded:"+key);
              }
            }

            @Override
            public void onCancelled(DatabaseError error) {

            }
        });

要在您的代码中使用有效数据,您需要从 dataSnapshot object 中获取数据,而不是使用 onChildAdded() 中的字符串 s方法。

正如您在 official doc 中看到的那样,String s 实际上代表 String previousChildName,这就是您在代码中使用之前的 child.[=16= 的方式]

希望对您有所帮助。