Ad

Adding Listener To A Value Which Doesn't Exist

I recently launched my android app which uses a Firebase realtime database and unexpectedly the bill was very huge because of the GB downloaded in Firebase which was $1/GB.
My app is on hold right now and I'm trying to fix it as soon as I can. I used to not add listeners at the bottom and always added at the root which caused this.

A

mDatabaseReference.child("Data").child(ID).child("Details").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                       //Do Something
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });

B

mDatabaseReference.child("Data").child(ID).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(dataSnapshot.hasChild("Details")){
                        //Do Something
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });

The first method doesn't give an error if the data doesn't exist but I want to know whether its searching in the database (Which adds up to GB downloaded) or not?

So is there any difference between A and B or is A better (if it doesn't add up to GB downloaded)

The google documentation doesn't tell much about listeners. Does adding listeners often in my app add up to GB downloaded? Also is there anything I can do about Connection Protocol and SSL Encryption?

Ad

Answer

It is implied that adding a listener will cause data to be downloaded from Realtime Database (this is the only provided way that data can be downloaded from your app using the SDK). If the data is already cached on the client, less data will be needed for the response.

A causes less overall data to be downloaded than B, because A is more specific than B. B will cause all children under it to be downloaded.

Ad
source: stackoverflow.com
Ad