【问题标题】:Firebase why onChildAdded parameter s returns nullFirebase为什么onChildAdded参数s返回null
【发布时间】: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 获得了有效数据,但是String s 参数的意义是什么?
  • 请看我的回答和String s的解释。

标签: java android firebase firebase-realtime-database firebase-security


【解决方案1】:

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

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

希望对你有帮助。

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 不合适,因为这将转储所有孩子,只是为了对孩子进行简单的更改。
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-26
    相关资源
    最近更新 更多