user3833732
user3833732

Reputation: 901

What is the difference between-Firebase Db Query snapshot and regular DbRef snapshot

Wanted to know is there a particular reason or benefit to use the Query dataSnapshot, rather than the regular DataBase Reference dataSnapshot. Please see the example- where I am trying to find information about a player via playerName. My Json Hierarchy goes like this:-

Player >> PlayerId(firebase generated Id) >>{playerName, playerAge, playerScore}

I can do this both ways OPTION 1... with Query

 gamesDBref = FirebaseDatabase.getInstance().getReference("Players");
    Query query = gamesDBref.orderByChild("playerName").equalTo("FlashBoy");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.e("R&D", "toString()" + dataSnapshot.toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

OPTION 2... with Database Reference

gamesDBref = FirebaseDatabase.getInstance().getReference("Players");
gamesDBref.orderByChild("playerName").equalTo("FlashBoy").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Log.e("R&D", "toString()" + dataSnapshot.toString());
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

Both my results are the same, But is one of them an inefficient way? Thanks it advance for the help/knowledge.

Upvotes: 0

Views: 878

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

The two pieces of code accomplish exactly the same in exactly the same way. With just the current snippets, they will even create the exact same number of objects.

If you need use the same Query in multiple places, then your first approach allows you to re-use the same Query object. That would reduce memory usage.

But to be honest, it's a micro-optimization that is unlikely to help in any substantial way.

Upvotes: 1

Related Questions