OrderByChild不会下订单,Firebase实时数据库 [英] OrderByChild wont order, Firebase Realtime Database

查看:99
本文介绍了OrderByChild不会下订单,Firebase实时数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Firebase实时数据库. 我在数据库中有以下排行榜数据:

I use Firebase Realtime Database. I have the following leaderboard data in the Database:

Leaders:
        0
           Name: Michal
           Score: 40
        1
           Name: David
           Score: 35
        2
           Name: Rob
           Score: 53

我正在读取除OrderByChild之外无法正常工作的数据. 我认为这与我保存数组编号有关.

I am reading the data which works fine besides the OrderByChild won't sort. I assume it is related to me saving with Array numbering.

注意:我不使用任何奇怪的方法来保存带有数组的数据,但实际上是使用Unity与Transactions进行交易的google示例,话虽如此,我希望OrderByChild能够正常工作. 交易,请参见屏幕底部

NOTE: I do not use any bizzare method to save the data with array, but actually google example using Transactions with Unity, having said that, i expect the OrderByChild to work. Transactions, see bottom of screen

这是检索数据的代码:

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

我确实尝试添加onIndex规则,但仍然相同,它忽略了顺序.

I did try to add the onIndex rule, but still the same, it ignores the ordering.

推荐答案

当您像这样查询数据库时:

When you query the database like this:

FirebaseDatabase.DefaultInstance
      .GetReference("Leaders").OrderByChild("score").GetValueAsync().ContinueWith(task =>

Firebase以正确的顺序返回快照,其中包含请求信息.但是当您随后致电:

Firebase returns a snapshot with the request information in the right order. But when you then call:

string JsonLeaderBaord = task.Result.GetRawJsonValue();

它必须将快照转换为JSON数据.而且在JSON中,子节点的顺序根据定义是不确定的,因此您会丢失有关该顺序的信息.

It has to convert the snapshot to JSON data. And in JSON the order of child nodes is by definition undefined, so you lose information about the order.

要以正确的顺序处理排行榜,请使用内置方法遍历快照:

To process the leaderboard in the correct order, use the built-in methods to loop over the snapshot:

foreach (DataSnapshot leader in snapshot.Children) {
    Debug.Log("Received value for leader: "+leader.Child("Score").Value);
}

这篇关于OrderByChild不会下订单,Firebase实时数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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