Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to fire up a coroutine from a lambda.

Discussion in 'Scripting' started by elopez7, Jun 11, 2019.

  1. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    I have this piece of code where I am trying to run some routines checks when a user signs up to my little toy application, however I discovered that the coroutine is not being executed.

    Code (CSharp):
    1. public void SignupNewUser(string email, string password)
    2.     {
    3.         auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
    4.         {
    5.             Debug.Log("ContinueWith Method called"); //This gets executed no problem.
    6.             StartCoroutine(authCallback(task, "Sign_Up"));//This is being completely ignored.
    7.         });
    8.     }
    I tested the coroutine by calling it directly from a different method and it works, it does get called.

    As always thank you so much for your help.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Are there any error messages in console? Are you sure your callback is running on main thread? If not, coroutine wont start.
     
  3. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    Hi, There are no errors showing up on the console, however I don't know how to make sure if the callback is running on the main thread, once I find out and check I will get back to you. Thank you.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    System.Threading.Thread have an Id property to check in CurrentThread. Also, the game object your script is attached to might be destroyed atm when callback fires. Also, show the definition for authCallback
     
  5. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    Thank you for the information.
    Effectively, the callback is not running on the main thread... Trying to figure out how to make it run on the main. Now the definition for authCallback is mostly empty.

    As I said before, I called that coroutine directly and it works.

    Code (CSharp):
    1. IEnumerator HandleAuthCallBack(Task<Firebase.Auth.FirebaseUser> task, string operation)
    2.     {
    3.         Debug.Log("Running Coroutine");
    4.         yield return null;
    5.     }
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You need to pass value from aside thread to main thread without stopping or blocking main thread. In .NET that's easy because

    This means you may wite in one and read in another thread any primitive type (int,bool,and alike) wthout any thread syncronization. You can create a boolean variable "private bool authCompleted = false", set it from your callback (aside thread) thread and read from main thread. In your coroutine you may wait until it becomes true and then use result/

    The tricky part is to save result to class field prior setting the flag because result is not primitive type and cannot be safely accessed from two or more thread without syncronization.
     
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,992
    This is where my failed coroutines usually happened. StartCoroutine needs to be directly called through a gameObject. If gameObject A calls some regular class B which tries to start a coroutine on A's behalf, it won't work. I think it won't give an error - just won't do anything. I've never tried it with lambda functions.

    As a test whether that's the issue, could give your gameObject a function to run the coroutine, like: void runSignUpCoroutine() { StartCoroutine(...); }. Then have the lambda function just call that.
     
  8. rozatlab

    rozatlab

    Joined:
    Dec 7, 2018
    Posts:
    6
    @elopez7 were you able to figure this out? I think I'm watching the same firebase tutorial with this exact issue and haven't a clue how to resolve it. Any help is appreciated!!
     
  9. amiraldev

    amiraldev

    Joined:
    Apr 7, 2020
    Posts:
    2
    I am having the same problem . Have you found a solution for this issue .

    Thanks in advance .
     
  10. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    @rozatlab and @amiraldev
    I am so sorry for not getting back to you. I got lost on other things and for some reason I never got notifications. I eventually did solve the issue.


    Code (CSharp):
    1. public void SignUpNewUser(string username, string email, string password)
    2.         {
    3.             auth.auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
    4.             {
    5.                 if(task.IsCanceled)
    6.                 {
    7.                     m_UISYstem.SwitchScreens(m_ErrorScreen);
    8.                     return;
    9.                 }
    10.                 if(task.IsFaulted)
    11.                 {
    12.                     Debug.LogErrorFormat("Error : {0}", task.Exception);
    13.                     m_UISYstem.SwitchScreens(m_ErrorScreen);
    14.                     return;
    15.                 }
    16.  
    17.                 //Firebase user has been created
    18.                 Firebase.Auth.FirebaseUser newUser = task.Result;
    19.  
    20.                 //Instantiate and Initialize Scriptable Object
    21.                 //appUser = ScriptableObject.CreateInstance<AppUser>();
    22.  
    23.                 appUser.InitUser(newUser.UserId, username, newUser.Email);
    24.                 //Set the username on the server side
    25.                 UpdateUserProfileAsync(newusername: username);
    26.  
    27.                 //Move to the create report screen
    28.                 m_UISYstem.SwitchScreens(m_CreateReportScreen);
    29.  
    30.             }, TaskScheduler.FromCurrentSynchronizationContext());//This is the line that made it all work
    31.         }
     
  11. amiraldev

    amiraldev

    Joined:
    Apr 7, 2020
    Posts:
    2
    Thanks @elopez7 , you saved me a lot of time!
     
  12. jste6370

    jste6370

    Joined:
    Feb 9, 2021
    Posts:
    2
    Wow thanks! I had to use this import too
    Code (CSharp):
    1. using System.Threading.Tasks;