-2

I'm using Firebase Realtime Database for storing and retrieving data for my Android application. On my home screen I have implemented addListenerForSingleValueEvent for reading from my database.My database has one value for example :

 "version" : 455

After listening from my leaf "version" i retrieve value "455" in my app correctly. Now i perform two steps :

  • Kill the app.
  • Manually update leaf from firebase console to 467.

After that when i launch the app i get the same old value "455" for the first time , on second launch i get the updated value "467".

Is this proper behaviour for firebase database ? Is there anyway i can get updated value from the first app launch ?

EDIT : Adding Code

  FirebaseDatabase database = FirebaseDatabase.getInstance();
  DatabaseReference ref= database.getReference();
  database.setPersistenceEnabled(true);
  ref.keepSynced(true);
  ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot != null) {
          long version = (long) dataSnapshot.child("version").getValue();
          // This is the value which doesn't update for the first time.
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
  });

P.S. : I have used ref.keepSynced(true); , still for the first time after updating manually gives the old value.

Community
  • 1
  • 1
Anonymous
  • 2,184
  • 15
  • 23

1 Answers1

1

Seeing your code I can say that contains no error. To solve the problem, just comment the following line of code:

database.setPersistenceEnabled(true);
ref.keepSynced(true);

And your problem will be solved. Your data will be displayed for the fist time.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193