Java Firebase按子值搜索 [英] Java Firebase Search By Child Value

查看:57
本文介绍了Java Firebase按子值搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个将条目存储在Firebase上的应用程序.每个条目都有一个author字段,用于指定作者.但是,每个帖子(无论作者如何)都将被推送到同一父级.有没有办法遍历该父项下的每个条目并查找给定作者的所有帖子?

I'm trying to make an application which stores entries on Firebase. Each entry will have an author field to specify who wrote it. However, every post (regardless of author) will be pushed to a common parent. Is there a way to iterate through each entry under this parent and find all posts by a given author?

推荐答案

您可以使用Firebase Query.基本上,无需下载所有数据库并自行过滤,而是查询某些孩子的值的FirebaseReference.您可以尝试类似的操作来检索同一位作者的所有帖子.

You can use a Firebase Query. Basically, instead of downloading all the database and filtering yourself, you query your FirebaseReference on certain children's value. You can try something like this to retrieve all posts from the same author.

// Get your reference to the node with all the entries
DatabaseRefenrec ref = FirebaseDatabase.getInstance().getReference();

// Query for all entries with a certain child with value equal to something
Query allPostFromAuthor = ref.orderByChild("name_of_the_child_node").equalTo("author_name");

// Add listener for Firebase response on said query
allPostFromAuthor.addValueEventListener( new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot post : dataSnapshot.getChildren() ){
            // Iterate through all posts with the same author
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});

为获得更好的性能,请考虑使用Firebase规则.这使得Firebase有序地保存数据,以便更快地管理查询.

For better performances, consider indexing you database using Firebase rules. This makes Firebase saving your data in an ordered way so that queries are managed faster.

这篇关于Java Firebase按子值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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