如何从firebase实时数据库中删除? [英] How to delete from firebase realtime database?

查看:144
本文介绍了如何从firebase实时数据库中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序中使用Firebase实时数据库,并具有这样的数据:



删除记录苹果(标记在图片中)?



根据文档,要删除您调用的一个项目 removeValue()关于参考。但是要获得参考,我需要孩子 id 。因为它是一个随机产生的id( -KISNx87aYigsH3ILp0D ),如何删除它? 不知道要删除的项的关键,你首先需要查询数据库来确定这些键:

  DatabaseReference ref = FirebaseDatabase.getInstance()。getReference(); 
查询applesQuery = ref.child(firebase-test)。orderByChild(title)。equalTo(Apple); (DataSnapshot appleSnapshot:dataSnapshot.getChildren())

applesQuery.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
{
appleSnapshot.getRef()。removeValue();
}
}
$ b $ @覆盖
public void onCancelled(DatabaseError databaseError){
Log.e(TAG,onCancelled,databaseError.toException());
}
});


I am using Firebase realtime database in Android app, and have data like this:

How can i delete the record "Apple" (marked in picture)?

According to the docs, to remove an item you call removeValue() on the reference. But to get the reference i require the child id. Because its a random generated id (-KISNx87aYigsH3ILp0D), how to delete it?

解决方案

If you don't know the key of the items to remove, you will first need to query the database to determine those keys:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query applesQuery = ref.child("firebase-test").orderByChild("title").equalTo("Apple");

applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {
            appleSnapshot.getRef().removeValue();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e(TAG, "onCancelled", databaseError.toException());
    }
});

这篇关于如何从firebase实时数据库中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆