Search Unity

Resolved Async Firebase code works in-editor but not on any devices.

Discussion in 'Scripting' started by EOMedvis, Jan 12, 2021.

  1. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    94
    I'm running into a problem where my Async code works in editor, but not on a mobile device.

    The code is as follows:


    Code (CSharp):
    1. using System;
    2. using System.Threading;
    3. using System.Threading.Tasks;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7. using Firebase;
    8. using Firebase.Auth;
    9.  
    10. public class FirebaseAuthenicator : MonoBehaviour
    11. {
    12.     public async Task SignInAnon()
    13.     {
    14.         await FirestoreController.firebaseAuth.SignInAnonymouslyAsync().ContinueWith(task =>
    15.         {
    16.             if (task.IsCanceled)
    17.             {
    18.                 Debug.LogError("SignInAnonymouslyAsync was canceled.");
    19.                 return;
    20.             }
    21.             if (task.IsFaulted)
    22.             {
    23.                 Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception);
    24.                 return;
    25.             }
    26.  
    27.             FirebaseUser anonUser = task.Result;
    28.             Debug.Log("User Signed-In");
    29.         });      
    30.     }
    31.  
    32.     public void SignOut()
    33.     {
    34.         FirestoreController.firebaseAuth.SignOut();
    35.         Debug.Log("User Signed-Out");
    36.     }
    37. }
    No problems on device when run from this:

    Code (CSharp):
    1. public async Task StartUp()
    2.     {      
    3.         await GetComponent<FirebaseAuthenicator>().SignInAnon();
    4.         FirestoreSaveNLoad.Load(); //loads previous carts.
    5.         await gameObject.GetComponent<AppUpdateChecker>().CheckForUpdates(); //checks loaded carts for updates.
    6.         GameManager.browserAssets = ResourceLoaderUnloader.LoadAssetBundle("Browser", "all-organization-logos-cart-icons"); //loads the asset bundle for browser images after the update.
    7.         GameManager.currentUser.AppStarts++;
    8.  
    9.         if(GameManager.currentUser.AppStarts == 1)
    10.         {
    11.             Debug.Log("First launch logged");
    12.             GameManager.currentUser.LaunchDate = DateTime.Now.ToString();
    13.            
    14.         }
    15.         else
    16.         {
    17.             GameManager.currentUser.RecentDate = DateTime.Now.ToString();
    18.         }
    19.  
    20.         //starts the main menu timer.
    21.         gameObject.GetComponent<SceneTimer>().Timer.Stop();
    22.         gameObject.GetComponent<SceneTimer>().Timer.Reset();
    23.         gameObject.GetComponent<SceneTimer>().Timer.Start();
    24.  
    25.         if(GameManager.allCarts.Count > 0) //if there is at least one cart installed, go to the carousel screen.
    26.         {
    27.             gameObject.GetComponent<MakeCarousel>().CreateCarouselCarts();
    28.             LocalProgressJSONManager.JSONM.AD.cartDownloaded = true;
    29.             LocalProgressJSONManager.JSONM.Save();
    30.         }
    31.         else //otherwise start the browser.
    32.         {
    33.             gameObject.GetComponent<FirestoreOrgBrowser>().StartOrgBrowser();
    34.         }
    35.  
    36.         FirestoreSaveNLoad.Save();
    37.     }
    However, when I run the function from a device. I get this null-exception error:


    AndroidPlayer(samsung_SM-G960U@192.168.1.19) NullReferenceException: Object reference not set to an instance of an object.
    at FirebaseAuthenicator+<SignInAnon>d__0.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0
    at FirebaseAuthenicator.SignInAnon () [0x00000] in <00000000000000000000000000000000>:0
    at AppStartUp+<StartUp>d__1.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0
    at AppStartUp.StartUp () [0x00000] in <00000000000000000000000000000000>:0
    at AppStartUp.Start () [0x00000] in <00000000000000000000000000000000>:0
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <00000000000000000000000000000000>:0
    at AppStartUp+<StartUp>d__1.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0
    at AppStartUp.StartUp () [0x00000] in <00000000000000000000000000000000>:0
    at AppStartUp.Start () [0x00000] in <00000000000000000000000000000000>:0
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0
    at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
    at UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () [0x00000] in <00000000000000000000000000000000>:0
    at UnityEngine.UnitySynchronizationContext.Exec () [0x00000] in <00000000000000000000000000000000>:0
    at UnityEngine.UnitySynchronizationContext.ExecuteTasks () [0x00000] in <00000000000000000000000000000000>:0
    UnityEngine.DebugLogHandler:LogException(Exception, Object)
    UnityEngine.Logger:LogException(Exception, Object)
    UnityEngine.Debug:LogException(Exception)
    UnityEngine.WorkRequest:Invoke()
    UnityEngine.UnitySynchronizationContext:Exec()
    UnityEngine.UnitySynchronizationContext:ExecuteTasks()

    (Filename: currently not available on il2cpp Line: -1)

    Any help would be much appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,751
    Since this works normally but not with your new-fangled async code, it certainly seems like an initialization error rather than a library binding error.

    Are you sure that all of the things in line 14 of your async example have been initialized by the time this runs? To prove it, print each one of them out with Debug.Log() BEFORE you use them and watch for them to happen before your exception.
     
  3. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    94
    Problem solved. Apparently I installing the unity packages manually as told to by Google's Firebase documentation wasn't the best way to install it. The I ended up with 2 different versions of Firebase in the same app that caused the conflict. Installing it through the Package manager was the way. This also fixed the null exception as well.
     
  4. unity_q_A7wvcewg-pdA

    unity_q_A7wvcewg-pdA

    Joined:
    Jan 18, 2020
    Posts:
    1
    but how you managed to delete the old ones