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屋!