问题描述
我正在创建我的第一个 Firebase 应用.它的要求之一是它在网络不可用时运行.Firebase 指南指出:
I’m creating my first Firebase App. One of its requirements is that it run when the network is not available. The Firebase guide states:
启用磁盘持久性允许我们的应用程序即使在应用程序重新启动后也能保持其所有状态.我们只需一行代码即可启用磁盘持久性.FirebaseDatabase.getInstance().setPersistenceEnabled(true);启用磁盘持久性后,我们同步的数据和写入将在应用重新启动时持久保存到磁盘,并且我们的应用应在离线情况下无缝运行.
另一个要求是使用 Google 登录.因此,在我的 MainActivity
中,我检查用户是否已登录,如果没有,则启动 SignInActivity
.(SignInActivity
来自 Firebase 示例.)SignInActivity
工作,用户登录,MainActivity
第二次启动.现在我的应用程序在代码行 FirebaseDatabase.getInstance().setPersistenceEnabled(true);
上崩溃,并显示以下消息:
Another requirement is to use Google Sign In. So in my MainActivity
I check if the User is signed in, if not, I launch the SignInActivity
. (The SignInActivity
is from the Firebase examples.) The SignInActivity
works, the user gets logged in, and MainActivity
is launched for a second time. Now my app crashes on the code line FirebaseDatabase.getInstance().setPersistenceEnabled(true);
with the following message:
必须在 FirebaseDatabase 实例的任何其他使用之前调用 setPersistenceEnabled().
现在,如果我重新启动我的应用程序,用户已登录,SignInActivity
未启动,我的应用程序运行良好.
Now if I restart my app, the User is signed in, the SignInActivity
is not launched, my app runs fine.
关于如何在用户登录后避免此崩溃的任何建议?
Any suggestions of how I avoid this crash after the User signs in?
当我发布这个问题时,我收到了重新定位 FirebaseDatabase.getInstance().setPersistenceEnabled(true);
的建议到我的应用程序类".我得到了完全相同的结果……SignInActivity
开始、完成,然后我在 setPersistenceEnabled
上崩溃.
As I was posting this question, I received a suggestion to relocate FirebaseDatabase.getInstance().setPersistenceEnabled(true);
to my "Application class". I get exactly the same result … SignInActivity
starts, completes, and I get a crash on the setPersistenceEnabled
.
下面是我的 MainActivity onCreate
:
Below is my MainActivity onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
// Crash here upon returning from SignInActivity.
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
mFirebaseDbReference = FirebaseDatabase.getInstance().getReference();
// Initialize Firebase Auth
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
if (mFirebaseUser == null) {
// Not signed in, launch the Sign In activity
Timber.tag("MainActivity").i("onCreate(): User not signed in, launching SignInActivity");
startActivity(new Intent(this, SignInActivity.class));
finish();
} else {
mUsername = mFirebaseUser.getDisplayName();
Timber.tag("MainActivity").i("onCreate(): User "%s" signed in.", mUsername);
if (mFirebaseUser.getPhotoUrl() != null) {
mPhotoUrl = mFirebaseUser.getPhotoUrl().toString();
}
}
推荐答案
FirebaseApp 由 ContentProvider
初始化,因此它在 onCreate()
时未初始化叫.
A FirebaseApp is initialized by a ContentProvider
so it is not initialized at the time onCreate()
is called.
像这样获取您的 FirebaseDatabase:
Get your FirebaseDatabase like this:
public class Utils {
private static FirebaseDatabase mDatabase;
public static FirebaseDatabase getDatabase() {
if (mDatabase == null) {
mDatabase = FirebaseDatabase.getInstance();
mDatabase.setPersistenceEnabled(true);
}
return mDatabase;
}
}
然后从您想要的任何活动中调用 Utils.getDatabase()
.
Then call Utils.getDatabase()
from any activity you want.
这篇关于setPersistenceEnabled(true) 使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!