Skip to content Skip to sidebar Skip to footer

Java Firebase Search By Child Value

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

Solution 1:

You can use a FirebaseQuery. 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 entriesDatabaseRefenrecref= FirebaseDatabase.getInstance().getReference();

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

// Add listener for Firebase response on said query
allPostFromAuthor.addValueEventListener( newValueEventListener(){
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot post : dataSnapshot.getChildren() ){
            // Iterate through all posts with the same author
        }
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
});

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.

Post a Comment for "Java Firebase Search By Child Value"