【问题标题】:Firebase GetValueAsync() if task.result doesnt exist it doesnt execute code belowFirebase GetValueAsync() 如果 task.result 不存在它不执行下面的代码
【发布时间】:2018-10-22 13:03:08
【问题描述】:

我在一个 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 ";
                }
            }
    );
}

【问题讨论】:

  • 为什么不等待任务var result = await CheckUser();如果您不等待任务,您的 UI 线程将挂起,直到它完成。这对手机来说很尴尬
  • 我真的不明白这个问题
  • 你看到 DataSnapshot snapshot = task.Result; 行后面的代码了吗,因为结束它需要时间,它会遍历它之后的所有代码而不执行它

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


【解决方案1】:
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";
                }
            });
    }

【讨论】:

    猜你喜欢
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    相关资源
    最近更新 更多