首页 > 解决方案 > Firebase GetValueAsync() 如果 task.result 不存在它不执行下面的代码

问题描述

我在 Unity 游戏上有这个功能,它在计算机上运行良好,但在移动设备上却没有。问题是 bool snap 将是错误的代码不会执行下面的任何行DataSnapshot snapshot = task.Result;。但如果DataSnapshot snapshot = task.Result;返回 1 个结果,它工作正常。

void CheckUser()
{
    reference.Child("Stars").Child("Users").Child(login).LimitToFirst(1)
        .GetValueAsync().ContinueWith(task =>
            {
                if (task.IsFaulted || task.IsCanceled)
                {
                    info.text = "falha";
                }
                else if (task.IsCompleted)
                {

                    //info.text = task.Result.ToString();
                    DataSnapshot snapshot = task.Result;
                    //info.text = snapshot.ToString();


                    bool snap = snapshot.Exists;
                    info.text = snap.ToString();


                    if (snap)
                    {
                        info.text = "login existente, selecione outro sff";
                    }

                    else
                    {
                        info.text = "falha 4 ";
                        writeLogin();
                    }
                    // Do something with snapshot...
                }
                else
                {
                    info.text = "falha 3 ";
                }
            }
    );
}

标签: c#firebaseunity3dfirebase-realtime-database

解决方案


void CheckUser()
    {


        reference.Child("Stars").Child("Users")
            .GetValueAsync().ContinueWith(task =>
            {
                if (task.IsFaulted || task.IsCanceled)
                {
                    info.text = "Verifique a Internet e tente de novo";
                }
                else if (task.IsCompleted)
                {

                    //info.text = task.Result.ToString();
                    IDictionary snapshot = (IDictionary) task.Result.Value;



                    bool snap = false;
                    if (snapshot[login]!= null)
                    {
                        snap = true;
                    }

                    Debug.Log(snap.ToString());


                    if (snap)
                    {
                        info.text = "login existente, selecione outro sff";
                    }

                    else
                    {
                        //info.text = "falha 4 ";
                        writeLogin();
                    }
                    // Do something with snapshot...
                }
                else
                {
                    info.text = "Verifique a Internet e tente de novo";
                }
            });
    }

推荐阅读