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?
The Firebase Realtime Database stores data returned from a query for use when offline.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With