1

I'm facing an issue regarding the Firebase Realtime database and in particular the value event listener which fires more than once .Seems that when the Internet state changes from on to off several times and the device finally has stable connection the onDataChange(DataSnapshot dataSnapshot) callback method of the listener is invoked with dataSnapshot of null content.Seems that the Realtime Database refers to the app's local cache and in that case I do not have any data stored in it. I am attaching the listener inside the Activity onStart() or when the device has established some connection ; I am detaching the listener inside the Activity onStop() method or when the device looses internet connection .Only one instance of a given listener exists at a time and every attach has corresponding detach action executed when needed. I have tried to wait a while between the change of the connection states before attaching the listener and to reattach the listener if the datasnapshot returns null .None of those worked.Please advice for a solution.

Some example code inside an Activity :

private ValueEventListener listener;
private Query query;
private boolean hasAttachedListener;

private Query getDatabaseReference() {

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    return reference.child(“some child ref”)
            .child(“other child ref 2 ”);
}

private ValueEventListener getDatabaseListener() {

    return new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Log.d(“firebase”, dataSnapshot.toString());
    //issue here datasnapshot is null sometimes
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d(“firebase”, databaseError.getDetails());
        }
    };
}


/**
 * Attaches  listener
 */
public void addListener() {

    if (!hasAttachedListener) {
        query = getDatabaseReference();
        listener = getDatabaseListener();
        query.addValueEventListener(listener);
        hasAttachedListener = true;
    }
}

/**
 * Detaches the attached listener
 */
public void removeListener() {

    if (hasAttachedListener) {
        query.removeEventListener(listener);
        query = null;
        listener = null;
        hasAttachedListener = false;
    }
}

@Override
protected void onStart() {
    super.onStart();
    addListener();
}

@Override
protected void onStop() {
    super.onStop();
    removeListener();
}

@Override
protected void onNetworkDisconnected() {
    super.onNetworkDisconnected();
    // invoked when internet connection is lost 
    removeListener();

}

@Override
protected void onNetworkReconnected() {
    super.onNetworkReconnected();
    // invoked when internet connection is restored 
    addListener();
}
metodieva
  • 131
  • 6
  • 2
    refer this link https://www.simplifiedcoding.net/firebase-realtime-database-crud/ –  Jul 10 '18 at 09:21
  • Hi the link provided is a guide .I have a working application with Firebase services! The problem is with the event listener caching information which is not correct .I have contacted the Firebase support reguarding this issue and it was validated by them that I am using the Realtime update functionality correctly but they couldn't help. – metodieva Jul 10 '18 at 09:40
  • Please add the code you are using. – Alex Mamo Jul 10 '18 at 10:36
  • @AlexMamo code added – metodieva Jul 10 '18 at 11:23
  • Have you tried to remove `onNetworkDisconnected` and `onNetworkReconnected` because those methods are not needed. – Alex Mamo Jul 10 '18 at 11:29

2 Answers2

0

With firebase offline capabilities you are not needed to use those two method to listen if there is no connection to the database

so your onNetworkDisconnected and onNetworkReconnected are not necesary

check the firebase docs here : https://firebase.google.com/docs/database/android/offline-capabilities

Keeping Data Fresh

The Firebase Realtime Database synchronizes and stores a local copy of the data for active listeners. In addition, you can keep specific locations in sync.

DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);

The Firebase Realtime Database client automatically downloads the data at these locations and keeps it in sync even if the reference has no active listeners. You can turn synchronization back off with the following line of code.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

The new data should have unique name to prevent replacement of existed