setPersistenceEnabled(true) 使应用程序崩溃 [英] setPersistenceEnabled(true) crashes app

查看:28
本文介绍了setPersistenceEnabled(true) 使应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我的第一个 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);启用磁盘持久性后,我们同步的数据和写入将在应用重新启动时持久保存到磁盘,并且我们的应用应在离线情况下无缝运行.

Enabling disk persistence allows our app to also keep all of its state even after an app restart. We can enable disk persistence with just one line of code. FirebaseDatabase.getInstance().setPersistenceEnabled(true); With disk persistence enabled, our synced data and writes will be persisted to disk across app restarts and our app should work seamlessly in offline situations.

另一个要求是使用 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().

Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.

现在,如果我重新启动我的应用程序,用户已登录,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) 使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆