【发布时间】:2017-05-04 10:33:32
【问题描述】:
我在我的 Android 项目中使用 Firebase(版本 10.0.0)并遇到以下 Firebase 数据库问题:
前提条件: 用户使用 Google 帐户通过 Firebase 身份验证登录(FirebaseAuth.getInstance().getCurrentUser() 返回非空值)。
- 在 Main Activity 的 onCreate 方法中,我从 Firebase 数据库中读取了一些值:
FirebaseDatabase.getInstance()
.getReference()
.child(NODE_USERS).child(user.getUid()).child(NODE_DICTIONARY_VERSION)
.addListenerForSingleValueEvent(...);
- 它工作正常。但是一段时间后,上述方法进入某种无限循环(没有错误,在 ValueEventListener 中没有 onCancelled 调用)。日志中还会出现以下消息:
W/PersistentConnection: pc_0 - 提供的身份验证凭据无效。 这通常表明您的 FirebaseApp 实例未正确初始化。 确保您的 google-services.json 文件具有正确的 firebase_url 和 api_key。 您可以从 https://console.firebase.google.com/ 重新下载 google-services.json
此后对 Firebase 数据库的所有其他调用(读取、写入、删除等)也不起作用。
为了解决这个问题,我尝试了
- 在访问 Firebase 数据库之前在应用程序启动时进行静默登录:
FirebaseAuth.getInstance().getCurrentUser() .reauthenticate(credential).addOnCompleteListener(...);
- 重新连接 Firebase 数据库:
FirebaseDatabase.getInstance().goOffline(); FirebaseDatabase.getInstance().goOnline();
因此,Firebase 数据库出现问题的频率并不高,至少在一天过去后才会出现。如果重新启动整个应用程序,问题也会消失一段时间(如果没有这些修复,问题每次都会重现,只有重新登录有帮助)。
无论如何,问题仍然存在,应用程序重启是一个非常糟糕的解决方案:)
知道如何解决这个问题吗?还是我做错了什么?
完整的 Firebase 日志:
12-19 13:03:40.544 D/FirebaseAuth: Notifying listeners about user ( HbY7R8FPR6QwgstQd3wmypI2nwJ2 ).
12-19 13:03:40.546 D/FirebaseApp: Notifying auth state listeners.
12-19 13:03:40.546 D/FirebaseApp: Notified 1 auth state listeners.
12-19 13:03:40.546 D/RepoOperation: Auth token changed, triggering auth token refresh
12-19 13:03:40.602 D/FirebaseAuth: Notifying listeners about user ( HbY7R8FPR6QwgstQd3wmypI2nwJ2 ).
12-19 13:03:40.613 D/FirebaseApp: Notifying auth state listeners.
12-19 13:03:40.613 D/FirebaseApp: Notified 1 auth state listeners.
12-19 13:03:40.613 D/RepoOperation: Auth token changed, triggering auth token refresh
12-19 13:03:43.832 V/FA: Session started, time: 176462962
12-19 13:03:43.853 I/FA: Tag Manager is not found and thus will not be used
12-19 13:03:43.858 D/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=MainActivity, _si=3824046701607023337}]
12-19 13:03:43.885 V/FA: Using measurement service
12-19 13:03:43.885 V/FA: Connecting to remote service
12-19 13:03:43.909 D/FA: Connected to remote service
12-19 13:03:43.910 V/FA: Processing queued up service tasks: 1
12-19 13:03:44.139 W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/.
12-19 13:03:45.985 W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/.
12-19 13:03:48.945 V/FA: Inactivity, disconnecting from the service
附:看起来问题只出现在我的 Nexus 5 (Android 6.0.1) 上。索尼 Xperia V (Android 4.3) 没有这个问题。
附言我还尝试构建调试和发布 apk,并将“编辑”角色添加到谷歌云控制台中所有自动生成的服务帐户 - 它也无济于事。
【问题讨论】:
标签: android firebase firebase-realtime-database