8

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?

kivmii
  • 178
  • 2
  • 10
  • Show your `updateUI` method and you are not using datasnapshot anywhere, how do you update your UI then – Shruti Jan 19 '18 at 11:48
  • I updated the code here. I save dataSnapshot and UI is updateing correctly. This is not my problem, but well noticed. – kivmii Jan 19 '18 at 12:35
  • Ok, so are you talking about wifi connection or internet connection? – Shruti Jan 19 '18 at 12:47
  • I do not know what prevents updates to the Firebase database or retrieving values when wifi is turned on. Wifi and internet connection is working fine with my devices except when using firebase database. – kivmii Jan 19 '18 at 13:19
  • try `addValueEventListener` instead of `addListenerForSingleValueEvent` – Shruti Jan 20 '18 at 05:35
  • did u solve your problem? I encountered a similar problem. No event firing over wifi (very good connection speed) – gbaccetta Apr 05 '18 at 22:45
  • No. I moved to use Firebase messaging. I have not encountered any connection problems with it. My app works in background so this is better solution for me. No need to worry about system killing my service. – kivmii Apr 23 '18 at 09:14

1 Answers1

11

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.

Freddie
  • 210
  • 3
  • 6
  • 1
    Bit dated, but I'd mark @Freddie's answer as the solution. I was banging my head against this one too. Did the same with my router - I restarted it, and now my app connects to Firebase RTD no problem. I'm wondering how I would let my users know to do this too, in case they have issues connecting for the same reason. – trod Sep 22 '18 at 02:36
  • 1
    Crazy, it's 2020 and I am just having this issue. I didn't think to restart the router - but it worked! Thanks for posting. Btw, which router do you have? Mine is D-LINK Model Name: DIR-890L – ezaspi Dec 06 '20 at 14:27
  • In my case I disabled IPv6 in my router. – malhobayyeb Feb 24 '22 at 23:09
  • Crazy, had the same issue!!! took me hours to understand i is a wifi issue – Avital Mar 27 '22 at 08:16