firebase / firebase-unity-sdk

The Firebase SDK for Unity
http://firebase.google.com
Apache License 2.0
199 stars 32 forks source link

[Bug] NullReferenceException when we call GoOffline from Unity's OnApplicationQuit() #1046

Open Awais6 opened 2 weeks ago

Awais6 commented 2 weeks ago

Description

Firebase sometimes throws NullReferenceException when we call GoOffline from Unity's OnApplicationQuit() in UnityEditor 2021.3.38f1 LTS.

I am using FirebaseAuth, FirebaseDatabase, FirebaseFirestore, FirebaseFunctions, FirebaseAnalyics

Reproducing the issue

Invoke GoOffline from firebaseDatabase instance inside OnApplicationQuit(). FirebaseDatabase should be connected and in use (like you can join any active listener on some node)

Firebase Unity SDK Version

11.9.0

Unity editor version

2021.3.38f1 LTS

Installation Method

.unitypackage

Problematic Firebase Component(s)

Database

Other Firebase Component(s) in use

Analytics, Authentication, Crashlytics, Database, Firestore, Functions

Additional SDKs you are using

UnityAds

Targeted Platform(s)

Android

Unity editor platform

Windows

Scripting Runtime

IL2CPP

Release Distribution Type

Pre-built SDK from https://firebase.google.com/download/unity

Relevant Log Output

NullReferenceException: Object reference not set to an instance of an object
Firebase.Database.FirebaseDatabase.GoOffline () (at /Users/runner/work/firebase-unity-sdk/firebase-unity-sdk/database/src/FirebaseDatabase.cs:257)

MRTDb.Database.DatabaseConnectionHandler.Dispose () (at <16dfc47d48274de88fe79b901c7083da>:0)
MRTDb.MonoBehavioursMultiplayerCallbacks.OnApplicationQuit () (at <16dfc47d48274de88fe79b901c7083da>:0)

If using CocoaPods for Apple platforms, the project's Podfile.lock

Expand Podfile.lock snippet
```yml 👀 Podfile.lock not needed, i am using Windows. ```
google-oss-bot commented 2 weeks ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

argzdev commented 1 week ago

Hey @Awais6, thanks for reaching out. I tried reproducing this behavior with our quickstart, and so far I'm not encountering any issues. That said, could you answer a few questions below so I can investigate this further?

  1. Could you clarify what you mean by this:

    Invoke GoOffline from firebaseDatabase instance inside OnApplicationQuit(). FirebaseDatabase should be connected and in use (like you can join any active listener on some node)

I understand that FirebaseDatabase can add active listeners, however, based on your comment, are you expecting the FirebaseDatabase to work even after invoking GoOffline?

  1. I'm curious about the use case, would you mind sharing why you have GoOffline in the OnApplicationQuit? Is there particular reason why you manually have to call this? From my understanding, there is already an automatic disconnection once the app closes.
Awais6 commented 1 week ago

Hi @argzdev, thanks for the quick response.

I am develping a multiplayer networking system with firebase database. Players create/join the room and pass data to each other in the room. i am using firebase database OnDisconnect.SetValue to set player's connectivity status as disconnected when there is a network problem.

When i use GoOffline() inside unity's OnApplicationQuit(), if any user minimize the game and clear it from recents on Android. GoOffline() is fired and OnDisconnect.Setvalue works instantly. Otherwise, If i don't use GoOffline() in unity's OnApplicationQuit() firebase servers detects player's network disconnection too late (after 60s to 90seconds).

Awais6 commented 1 week ago

Is GoOffline() automatically called, when User minimize the game/app and clear recent apps on Android?

argzdev commented 1 week ago

Hey @Awais6, thanks for the extra details. No, GoOffline() does not automatically call. I initially thought, you're only using GoOffline closing the app via OnApplicationQuit, I didn't take into account that you also consider minimizing the app.

There's a caveat in using OnApplicationQuit:

Warning: If the user suspends your application on a mobile platform, the operating system can quit the application to free up resources. In this case, depending on the operating system, Unity might be unable to call this method. On mobile platforms, it is best practice to not rely on this method to save the state of your application.

I believe using OnApplicationPause is a better way of handling this types of scenario for your use case.

Let me know if this helps. Thanks!

Awais6 commented 5 days ago

Hi @argzdev, Ok, I will try and reply back.