Code don't want to be execute in Async task =>

Hi everybody,

I’m using Firebase in my project , and i’m using the exemple code to connect User and it’s work Pefectly :

    public void ConnectionUserMAIL(string email, string password)
    {
        var auth = FirebaseAuth.DefaultInstance;
        auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
            if (task.IsCanceled)
            {
                Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
                return;
            }

            Firebase.Auth.FirebaseUser newUser = task.Result;
            Debug.LogFormat("User signed in successfully: {0} ({1})",
            newUser.DisplayName, newUser.UserId);
        });
    }

The problem is if I want to add a function, for example, after :

            if (task.IsCanceled)
            {
             // Script.instance.dosomething();
            
                Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
                return;

If I add " Script.instance.dosomethings() :"

Nothing happen :confused: … The code is good because I have the debug in console and no error message.

After that , I tried to create new function , with a other Void call, and a debug.log.
The debug of my call function is call, but nothings else …

EDIT : (After multiple test , I notice that Bool can be change ,but impossible to call function or execute code. )

    void Dosomething()
    {
        ok = true; // Bool Change !
        Debug.Log("Call test"); // Debug call !
        transform.gameObject.SetActive(false); // Transform nothing change ....
    }

Someone can explain me what i’m doing wrong ? :confused:

If you are here not on the main thread of Unity then instance related code isnt working, so check if “ConnectionUserMAIL” is executed in the main thread. If not (so i assume), declare a variable or get a variable that stores the result of “ConnectionUserMAIL” and process the result on the main thread of Unity (therefore in a normal monobehaviour c# class).

2 Likes

You schedule a continuation which will be executed when the task is done. However, there’s no guarantee that it executes on the desired thread. For almost anything Unity-related, you want to execute things on the main thread, not on any other thread.
In other words, the login probably executes on the another thread and continues there as well.

Unity’s logging works fine, because it doesn’t have any limitations in regards to threads, unlike all other parts of the engine’s API.

If you want to do something that needs to access the engine’s API, store the values and process them on the main thread.

2 Likes

Thanks @Zer0Cool @Suddoha !

I Understand, I didn’t know about “Main thread” and stuff, I’m a begginer with Async Task and I don’t really know what i’m doing ^^…

You said that I need to store the result of the task, and use it In the main Thread. I found a solution like but not very optimize.
I use a bool “ok”, set to true in the async and a Update() to check “ok”,
If ok, ok =fase, and dosomethings();

But I need to run a Update wich check a lot a different bool, There is more optimize way to achieve that?

Thanks for your help !

Dont know the framework but i would read this. Here he explains the usage of Coroutines in Unity to wait for the end of the firebase task:

1 Like

There are many ways. The usual thing you’d do with tasks is awaiting their completion (keyword await). There’re synchronization contexts involved, and if i recall correctly, they guys from Unity have implemented their own synchronization context since they wanted to support async/await, but for that it needs to be ensured that implicit continuations runs on the main thread - but this really only works when you await the tasks properly.

If that’s too advanced, you can still use the ways we suggested, by waiting for completion in Update or using coroutines, as @Zer0Cool mentioned. When you use async with await some day, you’ll notice that code written using these features somewhat is similarly structured like coroutines - but technically it’s still very different.

1 Like

Thanks to you ! I will deal with coroutine now ! :smile: