簡體   English   中英

為什么字符串在Firebase中返回null

[英]why string return null in firebase

//Anom loggin
    auth.SignInAnonymouslyAsync().ContinueWith(task =>
    {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInAnonymouslyAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception);
            return;
        }

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

        userdataString = newUser.UserId;
        Debug.Log("userdatastring: " + userdataString);
    });

    Debug.Log("userdatastring out: " + userdataString);
}

當它離開匿名函數時,該值返回null,但在其中,如果返回我需要獲取的ID,則知道如何獲取字符串值嗎?

該firebase函數是一個異步函數,這意味着內部回調將在異步函數完成之前(處於成功或失敗狀態)都不會運行

這意味着您的調試日志實際上是在運行異步函數的回調之前運行的。

以下是您可以做的一些選擇:

  1. 准備好后,讓Firebase異步函數調用回調函數。

     public void DoAnonymousSignIn(){ auth.SignInAnonymouslyAsync().ContinueWith(OnSignInDone); } private void OnSignInDone(Task<Firebase.Auth.FirebaseUser> task){ if (task.IsCanceled){ Debug.LogError("SignInAnonymouslyAsync was canceled."); return; } if (task.IsFaulted){ Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); userdataString = newUser.UserId; Debug.Log("userdatastring: " + userdataString); } 
  2. 通過內聯處理回調而不是使用回調函數來保持它現在的狀態,或者設置一個將其標記為就緒的變量,或者直接從中調用“ SignInDone”函數(例如SignInSuccess()和SignInFailed())

  3. 您還可以設置自己的回調事件,也可以從其他腳本中訂閱它們:

     using System; public static Action OnSignInSuccess; 

    然后在您的Firebase異步回調中,您可以執行以下操作:

      // ... within auth.SignInAnonymouslyAsync() callback Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); userdataString = newUser.UserId; if(OnSignInSuccess != null) OnSignInSuccess.Invoke(); } 

    然后其他腳本可以使用以下命令訂閱和取消訂閱此回調事件:

     void OnEnable(){ YourScript.OnSignInSuccess += SignInSuccess; } void OnDisable(){ YourScript.OnSignInSuccess -= SignInSuccess; } private void SignInSuccess(){ Debug.Log("userdatastring: " + YourScript.staticInstance.userDataString); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM