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

Aperta
#1,543 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
csharp, firebase, unity
Ambito
backend, mobile

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
C#
Stelle
325
Fork
62
Merge medio
1g 17h
PR unite (30g)
13

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di firebase/firebase-unity-sdk

Tutte le issue di firebase/firebase-unity-sdk

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.