kivmii
kivmii

Reputation: 308

Firebase database listeners don't work on android with wifi

In my code user signs in to Firebase with Google like explained in:

https://firebase.google.com/docs/auth/android/google-signin

This works fine.

When a user opens the program, it loads the initialization values from the firebase database. Here is the code:

private void loadPrefsFromDB() {
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    uid = user.getUid();
    FirebaseDatabase.getInstance().getReference().child("users").child(uid)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    userPrefs = dataSnapshot.getValue(UserPrefs.class);
                    updateUI(userPrefs);
                    Log.d(TAG, "loadPrefsFromDB:onDataChange");
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, "loadPrefsFromDB:onCancelled");
                }
            });
}

This works well when the wifi connection is turned off, but if i open the program wifi on, the function does not trigger. If, while the program is running, I click the wifi off and the phone switches to mobile data I instantly get login:

D / MainActivity: loadPrefsFromDB: onDataChange

The function also gets triggered if I sign out and again in with wifi on. Shouldn't firebase handle this situation? Or do I need to refresh authentication somehow?

Upvotes: 8

Views: 3934

Answers (1)

Freddie
Freddie

Reputation: 210

I was struggling with this issue for two days and found the real-time database just cannot work on the specified WIFI, that was my office's WIFI. It can work on the mobile data and even the hotspot signal from other mobile phones. I finally resolved it by restarting the WIFI router in our office, but cannot figure out the behind reason.

Upvotes: 11

Related Questions