【发布时间】:2021-04-02 15:20:14
【问题描述】:
我想更新最后一条消息,尝试了多种方法,但有点困惑更新最后一条消息的确切代码是什么。提前致谢。以下是我尝试过的代码:
databaseReference = FirebaseDatabase.getInstance().reference.child("user").child(apiKey)
databaseReference.orderByChild("api_key").equalTo(loginModel.api_key).addValueEventListener(object : ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
if(snapshot.exists()){
val map = mutableMapOf<String, String>()
map["last_message"] = message
map["last_message_key"] = apiKey
//databaseReference.child("api_key").child(loginModel.api_key).updateChildren(map.toMap())
//databaseReference.updateChildren(map.toMap())
databaseReference.child(snapshot.key!!).setValue(chatUser)
Utils.showToast(requireContext(), "Exists")
}else{
Utils.showToast(requireContext(), "Not Exists")
userReference.setValue(chatUser)
}
}
})
【问题讨论】:
-
您阅读过this part 的文档吗?如果有,具体有哪些不明白的地方?
-
你能把你试过的多种方法贴出来吗?文档中有关于在特定位置覆盖数据的非常具体的示例。
-
如果您已经尝试过某些方法但无法正常工作,请编辑您的问题以显示minimal code that any of us can run that reproduces where you got stuck。
-
@srinij 请检查代码。
标签: android firebase kotlin firebase-realtime-database