Firebase GetValueAsync()如果task.result不存在,则不执行以下代码

时间:2018-10-22 13:03:08

标签: c# firebase unity3d firebase-realtime-database

我在Unity游戏中具有此功能,该游戏在计算机上可以正常运行,但在移动设备上确实有效。 问题是布尔捕捉将是错误的,代码不会执行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 ";
                }
            }
    );
}

1 个答案:

答案 0 :(得分:0)

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