无法从Firebase实时数据库中检索其中一个嵌套数据

时间:2017-11-14 13:25:50

标签: android firebase firebase-realtime-database kotlin

我在firebase中有以下结构。

enter image description here

为了检索数据,我使用以下代码 -

 mFirebaseDatabaseReference = FirebaseDatabase.getInstance().reference.child("androidchat").child("users").child("uid-6")

mFirebaseDatabaseReference?.addValueEventListener(object : ValueEventListener {
    override fun onCancelled(p0: DatabaseError?) {
        callback?.handleData(false, requestType)
    }

    override fun onDataChange(p0: DataSnapshot?) {
        var userModel: UserModel? = p0?.getValue<UserModel>(UserModel::class.java)

        }
    }
})

当调用onDataChange()时,p0包含除数据之外的所有其他数据。

这是我的数据类UserModel -

data class UserModel(var uid: String? = null,
                 var name: String? = null,
                 var image_url: String? = null,
                 var user_location: String? = null,
                 var email: String? = null,
                 var group: MutableMap<String, Boolean> = mutableMapOf(),
                 var deviceIds : MutableMap<String,String> = mutableMapOf(),
                 var online: Boolean? = null,
                 var unread_group_count: Int? = null,
                 var last_seen_online: String? = null,
                 var last_seen_message_timestamp: String? = null,
                 var admin: Boolean? = null,
                 var delete_till: String? = null,
                 var active: Boolean? = null)

知道为什么数据不会出现?这是数据的屏幕截图,用户有很多组,但它显示0组 - enter image description here

这是我收到的数据快照 -

DataSnapshot { key = uid-6, value = {deviceIds={cc2583f5a2c92eff=dafO8mlj7aa:APA91bETLdet4b-oUnyYAe-GU68BuI4Sw1iT0jGCkUZdjes-ybA_9ZgHtZO-AoApVXQL9NfatyCgSfPSO74fsCUMY-6AY_8826D5AROVkFlQSUs8VKO5_FswoQceTxZ }, last_seen_message_timestamp=1510664398869, name=Akash Nidhi, uid=uid-6, online=true, user_location=USA-OTHERS, image_url=https://s3.ap-south-1.amazonaws.com/employees/images/150528664259b8d9f29c385.jpeg} }

数据快照中没有任何组!

更新 添加了这行mFirebaseDatabaseReference?.keepSynced(false)。它昨天工作了几次,现在还没有工作!

1 个答案:

答案 0 :(得分:0)

我也面临同样的问题。但最后,我得到了解决方案。我有以下格式的数据:

{
"name":"Deepak",
"date":"03 April 2018",
"match_number":"3",
"team_name1":"Delhi",
"team_name2":"Punjab"
}

从match_number,team_name1和team_name2 中删除了下划线(_),并使用了 matchNumber,teamName1和teamName2 。如下所示

 {
    "name":"Deepak",
    "date":"03 April 2018",
    "matchNumber":"3",
    "teamName1":"Delhi",
    "teamName2":"Punjab"
    }

它非常适合我。试试这个,也许它也适合你。