简体   繁体   中英

Firebase why onChildAdded parameter s returns null

I have a strange results while listening data.

For 3 children on node 'allstudents' the onchildadded method returns null for 0th elements however, assigns 0th pushid to 1st element and so on.

when I tweak,ID( -Kjw7V4jSphLBYs7Z_wx ) onChildChanged is triggered with null however, tweaking ID( -Kjw7bRY7AkJnGpOGJEw ) on FB-console onChildChange return differt ID( -Kjw7YchGZLamQmWTTc5 ) why?

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

在此处输入图片说明

rules:

"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() )"

}

adding data to node

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

retriving/listening data

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

To use in your code valid data, you need to get the data out from the dataSnapshot object in stead of using the String s from the onChildAdded() method.

As you see in the official doc , String s represents in fact String previousChildName and that's way you are having in your code the previous child.

Hope it helps.

Try this one

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) {

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM