How to delete a specific child from Firebase (java)? [duplicate]

孤街浪徒 提交于 2020-02-06 07:27:05

问题


I want to delete only the 1st child (L7jrJ6DtQWrmZsC4zvT) from Firebase database with an option in an Android app. I searched several places and could not find it. You only have one option to delete a whole database. Can anyone help?


回答1:


If i understood you correctly, you have the key of the child you want to remove and this child has no parent. so you can just use the single event listener like this -

val ref = FirebaseDatabase.getInstance().reference
        val applesQuery = ref.child("L7jrJ6DtQWrmZsC4zvT")
        applesQuery.addListenerForSingleValueEvent(
                object : ValueEventListener {
                    override fun onDataChange(dataSnapshot: DataSnapshot) {
                        dataSnapshot.ref.removeValue()
                    }
                    override fun onCancelled(databaseError: DatabaseError) {
                        Log.e("frgrgr", "onCancelled", databaseError.toException())
                    }
                })

this should work, let me know otherwise.




回答2:


To delete a single object you can use removeValue() method directly on the reference like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("calendario").child("-L7jrJ6DtQWrmZsC4zvT").removeValue();


来源:https://stackoverflow.com/questions/49361440/how-to-delete-a-specific-child-from-firebase-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!