[FR]: Public API to fully shut down Firebase (FirebaseHandler) when using Unity as a Library / Application.Unload
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by tracing FirebaseHandler initialization and termination in the Firebase.Platform runtime, then review how FirebaseApp and the Unity synchronization context are managed during Unity as a Library unload. Define a supported public shutdown and reinitialization lifecycle for named apps, including what callers must do before Application.Unload(). Done means the public API and lifecycle documentation cover clean reinitialization in the same process.
Written by the indexing model from the issue text.
Description
Description
We embed our Unity app inside a native host app using Unity as a Library (iOS and Android).
The user can open the Unity part, close it, and open it again during the same process lifetime.
When the user closes it, we unload the Unity player and load it again later.
We use Firebase App, Auth, Firestore and App Check, all with a named FirebaseApp
(FirebaseApp.Create(options, name)). We do this because the host app may use Firebase itself.
The problem: even after we dispose every public Firebase object, the static
Firebase.Platform.FirebaseHandler stays alive after the Unity unload. On the next launch of the
Unity part, the "Firebase Services" GameObject (and its UnitySynchronizationContext) is not
created again, because the SDK still thinks it is initialized. After that, callbacks such as
ContinueWithOnMainThread and Firestore listeners no longer work as expected.
Current workaround (reflection)
The only way we found to make re-initialization work is to call FirebaseHandler.Terminate()
through reflection before the "Firebase Services" GameObject is destroyed:
_firebaseAuth.Dispose();
_firebaseApp.Dispose();
// FirebaseHandler survives Application.Unload and prevents "Firebase Services"
// from being recreated on the next launch. Must be called before
// "Firebase Services" is destroyed so its UnitySynchronizationContext is reset too.
Type.GetType("Firebase.Platform.FirebaseHandler, Firebase.Platform")
?.GetMethod("Terminate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
?.Invoke(null, null);
We also have to add a link.xml so that IL2CPP managed code stripping keeps this method:
<linker>
<assembly fullname="Firebase.Platform">
<type fullname="Firebase.Platform.FirebaseHandler" preserve="all"/>
</assembly>
</linker>
This works, but it is fragile. It depends on an internal type and method name that can change
in any release without notice, and it has to be kept safe from code stripping by hand.
Steps to reproduce (without the workaround)
- Embed a Unity project using Firebase (App + Auth + Firestore) in a native app (Unity as a Library).
- Launch the Unity part, create a named
FirebaseApp, sign in anonymously, listen to a Firestore document. - Dispose
FirebaseAuth/FirebaseApp, then unload Unity (Application.Unload()/requestSelfUnload()). - Launch the Unity part again from the host app and initialize Firebase again.
- Observe that
"Firebase Services"is not recreated and that main-thread continuations / listeners do not fire.
Environment
- Firebase Unity SDK: 13.13.0 (App, Auth, Firestore, App Check, via UPM .tgz)
- Unity version: 6000.3.14
API Proposal
A supported, public way to fully shut down the Firebase Unity runtime so that it can be
initialized again cleanly in the same process. For example:
FirebaseApp.TerminateAll()/FirebaseApp.Shutdown(), which disposes all apps and resets
FirebaseHandler, the"Firebase Services"GameObject and the synchronization context; or- making
FirebaseHandler.Terminate()(or an equivalent) public inFirebase.Platform.
It would help if the docs also described the expected lifecycle for Unity as a Library:
what to call before Application.Unload(), and whether re-initialization in the same process
is supported
Firebase Product(s)
Firestore
Targeted Platform(s)
Android, Apple Platforms
- Dominant language
- C#
- Stars
- 325
- Forks
- 62
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 13
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from firebase/firebase-unity-sdk
-
[Bug] Desktop managed DLLs marked Required32Bit fail to load on ARM64 CoreCLR (Unity 7 alpha)Possibly taken @AustinBenoit claimed this today. Openapi: core desktop production type: bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
firebase/firebase-unity-sdk#1566 · 1 comment ·
Maintainers usually reply within 1 day
-
api: core
Difficulty 4/5 3-5 days Newbie friendliness 43/100
firebase/firebase-unity-sdk#1562 · 1 comment ·
Maintainers usually reply within 1 day
-
api: crashlytics type: question
Difficulty 4/5 3-5 days Newbie friendliness 45/100
firebase/firebase-unity-sdk#1560 · 2 comments ·
Maintainers usually reply within 1 day
-
[Bug] App Check (iOS): crash in TokenChangedMethod because AppCheckToken.expire_time_millis is left uninitializedPossibly taken @AustinBenoit claimed this 7 days ago. Openapi: appcheck type: bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
firebase/firebase-unity-sdk#1556 · 6 comments · 1 reaction · 1 assignee ·
Maintainers usually reply within 1 day
-
api: appcheck desktop production type: bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
firebase/firebase-unity-sdk#1530 · 3 comments ·
Maintainers usually reply within 1 day
All issues in firebase/firebase-unity-sdk
Similar issues
-
Support Flux ignore rules (drift detection ignore paths) in Microsoft.KubernetesConfiguration schemaOpen
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Azure/azure-resource-manager-schemas#3921 ·
Maintainers usually reply within 1 day
-
text-align: justify is ignored in HTML to PDF conversionPossibly taken @PrzemyslawKlys claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 4 days
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100