[FR]: Public API to fully shut down Firebase (FirebaseHandler) when using Unity as a Library / Application.Unload

Open
#1,543 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp, firebase, unity
Domain
backend, mobile

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

api: firestore type: feature request
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)

  1. Embed a Unity project using Firebase (App + Auth + Firestore) in a native app (Unity as a Library).
  2. Launch the Unity part, create a named FirebaseApp, sign in anonymously, listen to a Firestore document.
  3. Dispose FirebaseAuth / FirebaseApp, then unload Unity (Application.Unload() / requestSelfUnload()).
  4. Launch the Unity part again from the host app and initialize Firebase again.
  5. 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 in Firebase.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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from firebase/firebase-unity-sdk

All issues in firebase/firebase-unity-sdk

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.