Quick tips for threading in Unity with Firebase - Firecasts
Why Stack Overflow recommends it
Stack Overflow users link it when Firebase callbacks in Unity fail or behave oddly because Unity code is running on a background thread and ContinueWithOnMainThread is the fix.
Recommended by 3 people across 6 different months, 2020 to 2024.
What it is
A five-minute Firecasts episode from the Firebase channel in which developer advocate Patrick Martin explains threading pitfalls when using Firebase in Unity. It covers why ContinueWith can misbehave, the ContinueWithOnMainThread extension, async/await, and Unity coroutines.
What people said
Recommends Patrick Martin · 2020-05 · Stack Overflow · on “LoadImage(byteArray) doesnt work on unity”
Try replacing ContinueWith with ContinueWithOnMainThread . You're doing a lot of work in the UnityEngine namespace from a background thread, which can get sketchy. If you were following along from my video tutorial [LINK] , this is why I used coroutines. If that works, feel free to read my blog post [LINK] or watch my video [this video] on threading in Unity for more information about what's going on and what your other options are. --Patrick
Recommends Patrick Martin · 2020-06 · Stack Overflow · on “Cloud Firestore GetSnapshotAsync to return a value. Unity and C#”
Unfortunately, since Firestore is acting as a frontend for some slow running I/O (disk access or a web request), any interactions you have with it will need to be asynchronous. You'll also want to avoid blocking your game loop if at all possible while performing this access. That is to say, there won't be a synchronous call to GetSnapshotAsync . Now there are two options you have for writing code that feels synchronous (if you're like me, it's easier to think like this than with callbacks or reactive structures). First is that GetSnapshotAsync returns a task. You can opt to await on that task in an async function: public async bool CheckIfIsFullyRegistered(string idUtente) { DocumentReferenc
Recommends Patrick Martin · 2020-09 · Stack Overflow · on “How to read value from firebase in Unity?”
I wrote an article [LINK] and recorded a video [this video] on handling multitasking in Unity, and you should definitely check those out if you curious about all your options. For your issue specifically, I'd suggest simply changing ContinueWith to ContinueWithOnMainThread [LINK] . This is a drop in replacement. FirebaseDatabase.DefaultInstance.GetReference("Leaders").GetValueAsync().ContinueWithOnMainThread(task => { if (task.IsFaulted) { Debug.Log("error"); } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; Debug.Log(snapshot.Value.ToString()); } }); Which brings me to the explanation of what's happening: Something that's tricky about Firebase and Unity is that Firebase tends t
Recommends Patrick Martin · 2021-01 · Stack Overflow · on “unity app crashes, when signing in with google, using firebase”
It would be awesome if you could file any performance regressions with ContinueWithOnMainThread here: [LINK] (you'd need to demonstrate it with one of the quickstarts to get folks to look at it, but it would be good to track!). Otherwise, I did a writeup of multithreading strategies here: [LINK] (and a video: [this video] ) which has other things you can try. Maybe async/await or just Coroutines. It would require more re-architecting though.
Recommends Hellcat8 · 2022-08 · Stack Overflow · on “Why is part of my code not executed if I enable/disable Unity gameObject?”
So I've found a solution. As Sisus said (thanks for your answer, it helped me in my Google searches), this issue is caused by the code running on another thread instead of the main one. According to this Firebase video [this video] , when initializing Firebase, I can call ContinueWithOnMainThread instead of simply ContinueWith to solve that issue. (Note that this require using Firebase.Extensions , see Firebase.Extensions.TaskExtension docs [LINK] ) So updated Firebase initialization looks like this: Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => { var dependencyStatus = task.Result; if (dependencyStatus == Firebase.DependencyStatus.Available) { // Create and
Recommends syp5w2qxe · 2024-03 · Stack Overflow · on “FireBase database only updated when there is a Debug.Log”
It might have something todo with running the Task on the main thread or not. I recall from past experience that you should always run Firebase code on the main thread in Unity, by adding the Debug.Log() that's maybe happening behind the scenes and when omitted it gets run on a different thread. The google Firebase team discuss it here. [this video] . Apologies I don't have a blank Unity firebase project to hand to check the following syntax but I think you can do the above without the 'await' and Tasks with a MonoBehaviour. In the google documentation they don't use await. [LINK] [LINK] // can get the current user with Firebase.Auth.FirebaseUser user; Firebase.Auth.FirebaseAuth auth; // Handle i
Cites Patrick Martin · 2020-08 · Stack Overflow · on “How do you configure Firebase in Unity?”
I believe that the error message you're seeing is spurious. See this comment on GitHub [LINK] for example. Unless you're seeing any broken functionality, everything looks fine (farther down in your logs there's a Firebase initialized and messages about FCM tokens being properly received). Sometimes you do get issues using ContinueWith rather than ContinueWithOnMainThread (see this related blog post [LINK] or video [this video] ) which could obfuscate some logging or cause instability. From what I can see, you aren't running into that issue, but it's worth switching that around if ContinueWith wasn't a conscious decision.
Cites Patrick Martin · 2020-11 · Stack Overflow · on “Unity3D firebase AUTH not working on ANDROID”
If you don't mind, I'll try to add more context around your answer @sesmajster (and it's a little much to do in the comments). Your fix is to basically call CheckAndFixDependenciesAsync . What this is doing is that on Android, Firebase depends on Google Play Services (GPS) for many of its features. This call will ensure that GPS is available, and prompt the player to upgrade GPS if they're out of date. If the player decides to opt out of updating or has a device without access to GPS (in the US, most commonly the Kindle tablets or VR headsets), you'll get a DependencyStatus.Unavailable [LINK] result that you could take further action on. Note that on Android, GPS is not universally required
Cites syp5w2qxe · 2024-10 · Stack Overflow · on “Issue with Apple Sign In using Firebase in Unity”
It might be that you're using ContinueWith, instead of ContinueWithOnMainThread. Firebase on Unity must be called on the main thread to be safe. Can also 'await' in the method. See this video for reference. [this video]