Firebase 数据库侦听器在带有 wifi 的 android 上不起作用 [英] Firebase database listeners don't work on android with wifi
问题描述
在我的代码中,用户使用 Google 登录 Firebase,如下所述:
In my code user signs in to Firebase with Google like explained in:
https://firebase.google.com/docs/auth/android/谷歌登录
这很好用.
当用户打开程序时,它会从 firebase 数据库加载初始化值.代码如下:
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");
}
});
}
这在 wifi 连接关闭时效果很好,但如果我打开程序 wifi,该功能不会触发.如果在程序运行时,我点击关闭 wifi 并且手机切换到移动数据,我会立即登录:
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
如果我退出并在打开 wifi 的情况下再次登录,也会触发该功能.Firebase 不应该处理这种情况吗?还是我需要以某种方式刷新身份验证?
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?
推荐答案
我在这个问题上纠结了两天,发现实时数据库无法在指定的 WIFI 上工作,那是我办公室的 WIFI.它可以处理移动数据甚至来自其他手机的热点信号.我终于通过重启办公室的WIFI路由器解决了这个问题,但不知道背后的原因.
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.
这篇关于Firebase 数据库侦听器在带有 wifi 的 android 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!