本文介绍了通过操作回调从实时数据库检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Unity从Firebase的实时数据库接收JSON值.

I am trying to receive the JSON value from the Realtime Database of Firebase using Unity.

我执行以下操作:

   FirebaseDatabase.DefaultInstance
          .GetReference("Leaders").OrderByChild("score").GetValueAsync().ContinueWith(task =>
                    {
                        if (task.IsFaulted)
                        {
                            Debug.LogError("error in reading LeaderBoard");
                            return;
                        }
                        else if (task.IsCompleted)
                        {
                            Debug.Log("Received values for Leaders.");
                            string JsonLeaderBaord = task.Result.GetRawJsonValue();
                            callback(JsonLeaderBaord);
                        }
        }
  });

尝试读取回调:

private string GetStoredHighScores()
    {
      private string JsonLeaderBoardResult;
      DataBaseModel.Instance.RetriveLeaderBoard(result =>
            {
                JsonLeaderBoardResult = result; //gets the data

            });
  return JsonLeaderBoardResult; //returns Null since it doesn't wait for the result to come.
}

问题是我如何等待回调返回值,然后之后 返回 JsonLeaderBoardResult 的值.

Question is how do i wait for the callback to return the value and afterwards return the value of the JsonLeaderBoardResult.

推荐答案

RetriveLeaderBoard 函数不会立即返回.您可以使用协程等待它,也可以通过 Action 返回 JsonLeaderBoardResult 结果.在您的情况下,使用 Action 更有意义.

The RetriveLeaderBoard function doesn't return immediately. You can either use coroutine to wait for it or return the JsonLeaderBoardResult result via Action. Using Action make more sense in your case.

将字符串返回类型更改为void,然后通过 Action 返回结果:

Change the string return type to void then return the result through Action:

private void GetStoredHighScores(Action<string> callback)
{
    string JsonLeaderBoardResult;
    DataBaseModel.Instance.RetriveLeaderBoard(result =>
            {
                JsonLeaderBoardResult = result; //gets the data
                if (callback != null)
                    callback(JsonLeaderBoardResult);
            });
}

用法:

GetStoredHighScores((result) =>
{
    Debug.Log(result);
});

您收到此错误,因为 RetriveLeaderBoard 正在另一个线程上运行.从帖子中获取 UnityThread ,然后使用 UnityThread在主线程上进行回调.executeInUpdate .

You get this error because RetriveLeaderBoard is running from on another Thread. Grab UnityThread from this post then do the callback on the main Thread with UnityThread.executeInUpdate.

您的新代码:

void Awake()
{
    UnityThread.initUnityThread();
}

private void GetStoredHighScores(Action<string> callback)
{
    string JsonLeaderBoardResult;
    DataBaseModel.Instance.RetriveLeaderBoard(result =>
            {
                JsonLeaderBoardResult = result; //gets the data

                UnityThread.executeInUpdate(() =>
                {
                    if (callback != null)
                        callback(JsonLeaderBoardResult);
                });
            });
}

这篇关于通过操作回调从实时数据库检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:08