【问题标题】:why string return null in firebase为什么字符串在firebase中返回null
【发布时间】:2018-10-22 21:24:04
【问题描述】:
//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,知道如何获取字符串值吗?

【问题讨论】:

    标签: c# firebase unity3d firebase-authentication


    【解决方案1】:

    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);
      }
      

    【讨论】:

    • 3b。你也可以创建一个public UnityEvent onSignInSuccess; 并像Button 组件的onClick 回调一样配置它;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2019-05-04
    • 2020-09-24
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多