Alex9494
Alex9494

Reputation: 1778

Firebase database listener get values only when change

I have a node that I refresh every three days with some key-value pairs in my firebase realtime database. I want my app to get the new values only when I make changes in this node (so about every three days). I currently use this code to test :

    mFirebaseDatabase.getReference().child("test").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Long tsLong = System.currentTimeMillis();
                    Log.d("thatsdatasnapshot", "thatsdatasnapshot = " + tsLong.toString());
Log.d("thatsdatasnapshot", "thatsdatasnapshot = " + dataSnapshot.toString());
                }

My issue is that my logs in this listener print very often even I don't change anything in my node, for example everytime I start the app. So I guess that all the values of my node are downloaded "very often", not only when I make change in my database. Am I wrong ? How to download the values of my node only when there is a change ? I thought it was what addValueEventListener() should do but I have a doubt when I see my logs printed so often.

Thank you,

Alex

Upvotes: 0

Views: 2793

Answers (1)

JeobMallari
JeobMallari

Reputation: 50

The method addValueEventListener is only triggered when values in the realtime db change during your current run of the app, such as for every mRef.push() or mRef.child("someChild").setValue() method call in your app.

I had almost the same kind of problem with firebase realtime database and in the end, i resorted to creating an AsyncTask to update what you have to update during your app's runtime.

What you have to do is create an AsyncTask and make a REST call to your Firebase Database, and use it to update.

Upvotes: 2

Related Questions