PGMacDesign
PGMacDesign

Reputation: 6302

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

Working on an Android app that is using the new Firebase Database framework. It has data objects that are modeled like this: enter image description here 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!

Upvotes: 3

Views: 4029

Answers (1)

Nishant Dubey
Nishant Dubey

Reputation: 2872

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.

Upvotes: 4

Related Questions