How do I utilize filters and child elements in Google Firebase Database

纵饮孤独 提交于 2020-01-04 04:12:28

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/39735586/how-do-i-utilize-filters-and-child-elements-in-google-firebase-database

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