android setPersistenceEnabled()如果在应用程序处于后台时通过通知打开应用程序,则会出现错误

lpwwtiir  于 2023-01-24  发布在  Android
关注(0)|答案(1)|浏览(168)

我正在编写一个聊天应用程序。我已经在主Activity中启用了firebase数据库实时持久化。使用以下代码:

if(ref == null) {
            var database =
                FirebaseDatabase.getInstance(DB_URL)
            database.setPersistenceEnabled(true)
            ref = database.reference

            val chatRef = database!!.getReference("chats")
            chatRef.keepSynced(true)
        }

当我向用户发送消息时,用户点击通知,应用程序应该打开一个应用程序片段。如果应用程序关闭,它可以工作,如果它在后台,它会给我错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.modolo.healthyplus, PID: 16269
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.modolo.healthyplus/com.modolo.healthyplus.MainActivity}: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3763)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3937)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2288)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8293)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
     Caused by: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
        at com.google.firebase.database.FirebaseDatabase.assertUnfrozen(FirebaseDatabase.java:332)
        at com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(FirebaseDatabase.java:285)
        at com.modolo.healthyplus.MainActivity.onCreate(MainActivity.kt:72)
        at android.app.Activity.performCreate(Activity.java:8241)
        at android.app.Activity.performCreate(Activity.java:8207)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3736)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3937) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2288) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:210) 
        at android.os.Looper.loop(Looper.java:299) 
        at android.app.ActivityThread.main(ActivityThread.java:8293) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045) 

这是在日志中,因为我关闭应用程序,并离开它在后台:

D/DecorView[]: onWindowFocusChanged hasWindowFocus false
D/WebSocket: ws_0 - ws message: {"t":"d","d":{"b":{"p":"chats/kedgdOemYBP5XP0OuuMLYjpkyEe2f8OGIg86xxTM9v6OANszhwYnyRk1/messages/-NMUWfPTd-CqO3VPdinu","d":{"createdAt":"2023-01-23T16:47:05.005Z","message":"test","senderId":"f8OGIg86xxTM9v6OANszhwYnyRk1","type":"text"}},"a":"d"}}
D/WebSocket: ws_0 - Reset keepAlive. Remaining: 33012
D/WebSocket: ws_0 - HandleNewFrameCount: 1
D/WebSocket: ws_0 - handleIncomingFrame complete frame: {t=d, d={a=d, b={p=chats/kedgdOemYBP5XP0OuuMLYjpkyEe2f8OGIg86xxTM9v6OANszhwYnyRk1/messages/-NMUWfPTd-CqO3VPdinu, d={createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}}}}
D/Connection: conn_0 - received data message: {a=d, b={p=chats/kedgdOemYBP5XP0OuuMLYjpkyEe2f8OGIg86xxTM9v6OANszhwYnyRk1/messages/-NMUWfPTd-CqO3VPdinu, d={createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}}}
D/PersistentConnection: pc_0 - handleServerMessage: d {p=chats/kedgdOemYBP5XP0OuuMLYjpkyEe2f8OGIg86xxTM9v6OANszhwYnyRk1/messages/-NMUWfPTd-CqO3VPdinu, d={createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}}
D/WebSocket: ws_0 - ws message: {"t":"d","d":{"b":{"p":"chats/f8OGIg86xxTM9v6OANszhwYnyRk1kedgdOemYBP5XP0OuuMLYjpkyEe2/messages/-NMUWfQQ4fTWEa_tiPkn","d":{"createdAt":"2023-01-23T16:47:05.005Z","message":"test","senderId":"f8OGIg86xxTM9v6OANszhwYnyRk1","type":"text"}},"a":"d"}}
D/RepoOperation: onDataUpdate: /chats/kedgdOemYBP5XP0OuuMLYjpkyEe2f8OGIg86xxTM9v6OANszhwYnyRk1/messages/-NMUWfPTd-CqO3VPdinu
D/RepoOperation: onDataUpdate: /chats/kedgdOemYBP5XP0OuuMLYjpkyEe2f8OGIg86xxTM9v6OANszhwYnyRk1/messages/-NMUWfPTd-CqO3VPdinu {createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}
D/Persistence: Starting transaction.
D/Persistence: Persisted a total of 1 rows and deleted 0 rows for a set at /chats/kedgdOemYBP5XP0OuuMLYjpkyEe2f8OGIg86xxTM9v6OANszhwYnyRk1/messages/-NMUWfPTd-CqO3VPdinu in 3ms
D/Persistence: Transaction completed. Elapsed: 9ms
D/WebSocket: ws_0 - Reset keepAlive. Remaining: 44978
D/WebSocket: ws_0 - HandleNewFrameCount: 1
D/WebSocket: ws_0 - handleIncomingFrame complete frame: {t=d, d={a=d, b={p=chats/f8OGIg86xxTM9v6OANszhwYnyRk1kedgdOemYBP5XP0OuuMLYjpkyEe2/messages/-NMUWfQQ4fTWEa_tiPkn, d={createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}}}}
D/Connection: conn_0 - received data message: {a=d, b={p=chats/f8OGIg86xxTM9v6OANszhwYnyRk1kedgdOemYBP5XP0OuuMLYjpkyEe2/messages/-NMUWfQQ4fTWEa_tiPkn, d={createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}}}
D/PersistentConnection: pc_0 - handleServerMessage: d {p=chats/f8OGIg86xxTM9v6OANszhwYnyRk1kedgdOemYBP5XP0OuuMLYjpkyEe2/messages/-NMUWfQQ4fTWEa_tiPkn, d={createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}}
D/RepoOperation: onDataUpdate: /chats/f8OGIg86xxTM9v6OANszhwYnyRk1kedgdOemYBP5XP0OuuMLYjpkyEe2/messages/-NMUWfQQ4fTWEa_tiPkn
D/RepoOperation: onDataUpdate: /chats/f8OGIg86xxTM9v6OANszhwYnyRk1kedgdOemYBP5XP0OuuMLYjpkyEe2/messages/-NMUWfQQ4fTWEa_tiPkn {createdAt=2023-01-23T16:47:05.005Z, senderId=f8OGIg86xxTM9v6OANszhwYnyRk1, message=test, type=text}
D/Persistence: Starting transaction.
D/Persistence: Persisted a total of 1 rows and deleted 0 rows for a set at /chats/f8OGIg86xxTM9v6OANszhwYnyRk1kedgdOemYBP5XP0OuuMLYjpkyEe2/messages/-NMUWfQQ4fTWEa_tiPkn in 2ms
D/Persistence: Transaction completed. Elapsed: 4ms
W/olo.healthyplu: Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
W/olo.healthyplu: Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
W/olo.healthyplu: Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
W/olo.healthyplu: Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
W/System: A resource failed to call close. 
D/CompatibilityChangeReporter: Compat change id reported: 160794467; UID 10624; state: DISABLED
D/AppScoutStateMachine: 26520-ScoutStateMachinecreated

我不明白为什么错误只在某些情况下给我。

ecbunoof

ecbunoof1#

错误消息非常明确:
在使用FirebaseDatabase示例的任何其他用途之前,必须对setPersistenceEnabled()执行all。
因此,显然在某些情况下,您在已经使用数据库(从数据库读取或向数据库写入)之后调用setPersistenceEnabled(),这是不允许的。
我们不可能说出为什么这是来自您共享的代码,但是如果您记录数据库调用,那么应该可以相当快地发现流。

相关问题