Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 547
kivmii Avatar asked Jan 19 '18 11:01

kivmii


People also ask

Can Firebase realtime database work offline?

The Firebase Realtime Database stores data returned from a query for use when offline.

How much traffic can Firebase handle?

While there isn't a limit to how many read or write operations you can trigger from a single function, a single database write operation can only trigger 1000 functions, or 500 functions per region for Cloud Functions v2.

How do Firebase listeners work?

Firebase utilizes listeners to watch for changes in a specified node. It is similar to an event handler in the sense that a code is triggered based on a certain circumstance. In our case, whenever changes in that node's data occur, the listener automatically provides the application updated data, called a snapshot.


1 Answers

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.

like image 165
Freddie Avatar answered Nov 11 '22 16:11

Freddie