Firebase Google登录任务未在Unity中运行ContinueWith()方法 [英] Firebase Google Sign In task doesn't run ContinueWith() method in Unity

查看:99
本文介绍了Firebase Google登录任务未在Unity中运行ContinueWith()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 FirebaseAuth.unitypackage google-signin-plugin-0.1.4.unitypackage 导入到我的项目中,并作为登录管理器编写了以下代码:

I imported FirebaseAuth.unitypackage and google-signin-plugin-0.1.4.unitypackage into my project and wrote the following code as a signin manager:

using System;
using System.Threading.Tasks;
using Firebase.Auth;
using Google;
using UnityEngine;

public class GoogleSignInManager : MonoBehaviour
{
    public static GoogleSignInManager Instance;
    public GoogleSignInManager() => Instance = this;

    private FirebaseAuth _auth;
    public FirebaseUser CurrentUser => _auth?.CurrentUser;

    private void Awake()
    {
        _auth = FirebaseAuth.DefaultInstance;
        GoogleSignIn.Configuration = new GoogleSignInConfiguration
        {
            RequestIdToken = true,
            WebClientId = "xxxx"
        };
    }

    public Task<FirebaseUser> SignIn()
    {
        var signIn = GoogleSignIn.DefaultInstance.SignIn();
        var signInCompleted = new TaskCompletionSource<FirebaseUser>();
        signIn.ContinueWith(task =>
        {
            Debug.Log($"In signIn ContinueWith (status = {task.Status:g})");
            if (task.IsCanceled)
                signInCompleted.SetCanceled();
            else if (task.IsFaulted)
                signInCompleted.SetException(task.Exception ?? new Exception("Task Is Faulted"));
            else
            {
                var signInUser = task.Result;
                var credential = GoogleAuthProvider.GetCredential(signInUser.IdToken, null);
                _auth.SignInWithCredentialAsync(credential).ContinueWith(authTask =>
                {
                    Debug.Log($"In SignInWithCredentialAsync ContinueWith (status = {authTask.Status:g})");
                    if (authTask.IsCanceled)
                        signInCompleted.SetCanceled();
                    else if (authTask.IsFaulted)
                        signInCompleted.SetException(authTask.Exception ?? new Exception("Task Is Faulted"));
                    else
                        signInCompleted.SetResult(authTask.Result);
                });
            }
        });
        return signInCompleted.Task;
    }

    public void SignOut()
    {
        _auth.SignOut();
        GoogleSignIn.DefaultInstance.SignOut();
    }
}

这可以正常工作并在android中显示Google登录窗口,但是问题是,在我选择电子邮件后,它只是关闭并 Debug.Log($"在SignIn ContinueWith(status = {task.Status:g})); 根本不会被调用.我无法弄清楚我在做什么错,因为我没有收到任何错误,但这根本行不通.我想知道我做错了什么以及如何解决该问题.

This works and shows the Google SignIn window in android, but the problem is that after I select my email it just closes and Debug.Log($"In signIn ContinueWith (status = {task.Status:g})"); doesn't get called at all. I can't figure out what I'm doing wrong as I get no errors and it just doesn't work. I'm wondering what did I do wrong and how can I fix the issue.

更新1

return signInCompleted.Task; 之前添加了 signIn.Wait(); ,现在由于任务尚未实际完成,我的应用程序冻结了.

Added signIn.Wait(); before return signInCompleted.Task; and now my application freezes as the task doesn't actually complete.

更新2

如评论部分所述,我检查了日志猫,发现我的 Debug.Log(); 已在其中执行,并且对 IngameDebugConsole 不可见.任务完成, ContinueWith()方法实际运行.但是现在问题出在以下代码中:

As mentioned in the comments section, I checked the logcat and found that my Debug.Log(); has executed in there and is not visible to the IngameDebugConsole. So the task completes and the ContinueWith() method actually runs. But now the problem is in the following code:

GoogleSignInManager.Instance.SignIn().ContinueWith(task =>
{
    if (task.IsCanceled)
        Debug.Log("Please Try Again!");
    else if (task.IsFaulted)
        Debug.LogError(task.Exception?.Message);
    else
    {
        var user = task.Result;
        Debug.Log(user.Email);
        email.text = user.Email;
    }
});

Debug.Log(user.Email); 或任何其他主线程方法未运行.我想在 UnityEngine.UI.Text 中显示用户的电子邮件地址,但不会被调用.

The Debug.Log(user.Email); or any other main thread method doesn't run. I want to show user's email address in a UnityEngine.UI.Text but it doesn't get called.

推荐答案

ContinueWith 不能保证在Unity的"main"上执行.线程.

ContinueWith is not guaranteed to be executed on the Unity "main" thread.

由于您始终拥有特定于Unity的Firebase,因此请使用

Since you have the Firebase anyway specific for Unity rather use the ContinueWithOnMainThread from the Firebase TaskExtensions

GoogleSignInManager.Instance.SignIn().ContinueWithOnMainThread(task =>
{
    if (task.IsCanceled)
        Debug.Log("Please Try Again!");
    else if (task.IsFaulted)
        Debug.LogError(task.Exception?.Message);
    else
    {
        var user = task.Result;
        Debug.Log(user.Email);
    }
});

这篇关于Firebase Google登录任务未在Unity中运行ContinueWith()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆