Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

android - Unable to delete Firebase Child Node

I have built a chat application based on Firebase Realtime. I am working on deleting the Chatlist and related messages. Currently, I have separate Nodes for Chats with different users for both Sender and receiver, stored in ChatLists node. Messages in these chats stored in another node (Chats). Structure below

Firebase DB

The logic to delete chats being:

  1. Delete selected Chat Window for the logged in user when delete approved
  2. Check if the Receiver has also deleted their chat windor?
  3. If yes, then delete all messages - stored together in the Chats node - identified by ChatKeyID

Now I am able to delete the ChatList Window but unable to delete the Messages when both sender and receiver chat windows have been deleted using the code below - removeValue for "Chats" is not working - can anybody please guide as to what is wrong with the code here?

   private fun deleteChatList(
    holder: ViewHolder,
    chatKeyIdDel: String?,
    receiverId: String
) {
    val userId = FirebaseAuth.getInstance().currentUser!!.uid
    val chatListRefR = FirebaseDatabase.getInstance().reference.child("ChatList").child(receiverId).child(userId)
    val chatListRefS = FirebaseDatabase.getInstance().reference.child("ChatList").child(userId).child(receiverId).removeValue()
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                chatListRefR.addListenerForSingleValueEvent(object : ValueEventListener {
                    override fun onDataChange(snapshot: DataSnapshot) {
                        val chats = snapshot.getValue(MessageList::class.java)
                        if (!snapshot.exists()) {
                            Toast.makeText(holder.itemView.context, "$chatKeyIdDel", Toast.LENGTH_LONG).show()
                            val chatRef =
                                FirebaseDatabase.getInstance().reference.child("Chats").child(chatKeyIdDel).removeValue()
                        }
                    }

                    override fun onCancelled(error: DatabaseError) {
                        TODO("Not yet implemented")
                    }

                })
                Toast.makeText(
                    holder.itemView.context,
                    "Conversation has been deleted",
                    Toast.LENGTH_LONG
                ).show()
            } else {
                Toast.makeText(
                    holder.itemView.context,
                    "Conversation was not deleted",
                    Toast.LENGTH_LONG
                ).show()
            }
        }

}
question from:https://stackoverflow.com/questions/65916804/unable-to-delete-firebase-child-node

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...