如何在Google Firebase数据库中使用过滤器和子元素 [英] How do I utilize filters and child elements in Google Firebase Database

查看:230
本文介绍了如何在Google Firebase数据库中使用过滤器和子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用新的Firebase数据库框架的Android应用上工作。它有像这样建模的数据对象:

顶级父母(1234-4321)是聊天室,数据对象是聊天消息和编号项目(0,1,2)是个人消息。



我可以毫无困难地得到整个数据库,并通过监听器读取:

  FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference();
myRef.addChildEventListener(this);
myRef.addValueEventListener(this);

我可以用这种方式得到一个孩子:

  FirebaseDatabase database = FirebaseDatabase.getInstance(); 
String id =1234-4321;
DatabaseReference myRef = database.getReference()。child(id);
myRef.addChildEventListener(this);
myRef.addValueEventListener(this);

但是我不知道如何获取同一类型的多个对象。我的意思是,一个用户将能够获得多个聊天室(IE,包括'1234-4321'和'1234-4432'),但我唯一能看到的方法是: 1)通过onChildAdded或onDataChange侦听器循环,通过匹配字符串id来分离出项目并更新它们。然而,这是非常低效的,因为我解析整个数据库,这可能是相当大的

  @Override 
public void onChildAdded(DataSnapshot dataSnapshot,String s){$ b $ if(dataSnapshot!= null){
try {
ChatObjectV2 objectV2 =(ChatObjectV2)dataSnapshot.getValue();
//在这里查找id并循环遍历所有内容
catch(Exception e){
e.printStackTrace();


$ b code $
$ b $或$ $

2)添加一个特定的孩子,但是如果我尝试添加更多的孩子,当我想要更宽的时候,它将会更深入到嵌套的对象中。

  //这不会工作,因为它会更深而不是更宽
String id = 1234-4321;
String id2 =1234-4432;
Query myQuery = myRef.child(id).child(id2);

然后以相同的方式在侦听器中循环,但是,我需要创建一个不同的DatabaseReference对于每个聊天室来说,这是非常低效的。



看起来解决方案可能是使用过滤器,但是我不知道如何在现有的FirebaseDatabase和DatabaseReference对象中使用它们。有没有人有任何想法如何做一个过滤器工作方面的数据模式/模型我已经在这里列出?



感谢您的帮助! 我会尽力解释你在这些例子中过滤的基本用法:
$ b $ p获取前两个聊天室
$ b
$ b

 查询chatRoomsQuery = databaseReference.limitToLast(2); 

//得到最后两个聊天室

 查询chatRoomsQuery = databaseReference.limitToFirst(2)

//获取所有活动ID

$ p $查询chatRoomsQuery = databaseReference.orderByChild(激活)equalTo(真);

这只是一个基本的示例,我鼓励您通过这个博客。这惊人地解释了高级查询。

或者,您也可以通过这些文档。他们是不同于你分享。



请让我知道,如果这是你在找什么。

Working on an Android app that is using the new Firebase Database framework. It has data objects that are modeled like this: Where the Top parent (1234-4321) is the 'chat room', the data object are the 'chat messages', and the numbered items (0, 1, 2) are the 'individual message'.

I am able to get the entire Database without any trouble and read it via listeners:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
myRef.addChildEventListener(this);
myRef.addValueEventListener(this);

And I am able to get a single child in this fashion:

FirebaseDatabase database = FirebaseDatabase.getInstance();
String id = "1234-4321";
DatabaseReference myRef = database.getReference().child(id);
myRef.addChildEventListener(this);
myRef.addValueEventListener(this);

But I cannot for the life of me figure out how to get multiple objects of the same type. What I mean is, a user will be able to get More than one chat room (IE, both '1234-4321' and '1234-4432'), but the only way I can see to do this is either to:

1) loop through the onChildAdded or onDataChange listeners, separate out the items by matching the String ids, and updating them. This is, however, extremely inefficient as I am parsing the entire Database, which could be quite large

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    if(dataSnapshot != null){
        try {
            ChatObjectV2 objectV2 = (ChatObjectV2) dataSnapshot.getValue();
            //Check here for ids and loop through everything
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

or

2) To add a specific child, but if I try to add more children it is going 'deeper' into the nested object when I want it to go 'wider'.

//This won't work because it is going 'deeper' instead of 'wider'
String id = "1234-4321";
String id2 = "1234-4432";
Query myQuery = myRef.child(id).child(id2);

And then loop through in the listener the same way, but, I would need to create a different DatabaseReference for every chat room, which is horribly inefficient.

It looks like the solution is probably to use filters, but I cannot for the life of me figure out how to utilize them in the existing FirebaseDatabase and DatabaseReference objects. Does anyone have any idea how to make a filter work with regards to the data schema / model I have listed here?

Thanks for the help!

解决方案

I would try to explain you the basic use of filtering in these examples:

//getting first two chat rooms

Query chatRoomsQuery = databaseReference.limitToLast(2);

//getting last two chat rooms

Query chatRoomsQuery = databaseReference.limitToFirst(2)

//getting all active id

Query chatRoomsQuery = databaseReference.orderByChild("active").equalTo(true);

This is just a basic sample I would encourage you to go through this blog. Which explains advanced queries amazingly.

Alternatively, you can also go through these docs. They are different than what you shared.

Do let me know if this is what you were looking for.

这篇关于如何在Google Firebase数据库中使用过滤器和子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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