如何正确使用queryOrderedByValue [英] How to properly use queryOrderedByValue

查看:143
本文介绍了如何正确使用queryOrderedByValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  @IBAction func onShowLeaderboardTapped 

我有这个函数写在Swift中获取排行榜,然后显示给用户: (_ sender:Any){
let leaderboardDB = FIRDatabase.database()。reference()。child(scores)。queryOrderedByValue()。queryLimited(toLast:5)

leaderboardDB .observeSingleEvent(:.value,其中:{(快照)在
print(排行榜快照:快照)
},withCancel:nil)

}

问题在于,当我获取它时,它给了我下面的列表:

  Ben = 9; 
Gabriela = 12;
Ivailo = 7;
Petar = 10;
Vania = 10;

它显示了前五名玩家,但是按字母顺序排列。由于这不是一个大问题,我想知道是否有一种方法来定购他们的价值。

这些是规则:

  {
rules:{
.read:auth!= null,
.write: $ auth!= null,
scores:{
.indexOn:.value
}
}
}

而数据库的组织如下:

  matchrooms:
用户:
分数:
>本:9
> Dimitar:7
> Gabriela:12
>最高:6
> Ivailo:7
> Ivan:5
> Marina:3
> Pesho:3
> Petar:10
>罗森:6
> Vania:10
> Yasen:2

所以,我的问题是如何正确使用 queryOrderedByValue )来检索前五名球员,并按他们得分得分列出他们?

解决方案

当您触发对Firebase的查询时,它将返回与您的查询匹配的项目的键,这些项目的值以及结果中项目的相对顺序。如果你听一个.Value事件,所有这三个被合并成一个FIRDataSnapshot。



但是当你要求的值该快照的属性将快照打印为一个块,数据将转换为字典。由于字典只能包含键和值,因此这些项的排序会丢失。而事实证明,字典然后打印按键排序的项目。



要按顺序获取项目,您应该使用 snapshot.children

  leaderboardDB.observeSingleEvent(.value,与:{ )in 
for snapshot.children {
print(child.key)
}
},withCancel:nil)

另见:


I have this function written in Swift that fetch the leaderboard and then shows it to the user :

@IBAction func onShowLeaderboardTapped(_ sender: Any) {
        let leaderboardDB = FIRDatabase.database().reference().child("scores").queryOrderedByValue().queryLimited(toLast: 5)

        leaderboardDB.observeSingleEvent(of: .value, with: { (snapshot) in
            print("leaderboard snapshot:" ,snapshot)
        }, withCancel: nil)

    }

The problem is that when I fetch it, it gaves me the following list :

 Ben = 9;
 Gabriela = 12;
 Ivailo = 7;
 Petar = 10;
 Vania = 10;

It shows me the first five players, but then it lists them alphabetically. Since this is not a big problem I wonder if there is a way to order them by the value.

These are the rules:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
      "scores": {
      ".indexOn": ".value"
    }
  }
}

And the DB is organized as follows:

  matchrooms:
  users: 
  scores:
>      Ben: 9
>      Dimitar: 7
>      Gabriela: 12
>      Ishest: 6
>      Ivailo: 7
>      Ivan: 5
>      Marina: 3
>      Pesho: 3
>      Petar: 10
>      Rosen: 6
>      Vania: 10
>      Yasen: 2

So, my question is how to properly use the queryOrderedByValue() to retrieve the first five players and list them by the points they've scored?

解决方案

When you fire a query against Firebase, it returns the keys of the items that match your query, the value of those items and the relative order of the items in the result. If you listen for a .Value event, all three of these are combined into a single FIRDataSnapshot.

But when you then either ask for the value property of that snapshot or print that snapshot as one block, the data is converted into a Dictionary. Since a dictionary can only contain keys and values, the ordering of the items is lost at that point. And as it turns out, the dictionary then prints the items ordered by their key.

To get the items in order, you should iterate over them using snapshot.children:

leaderboardDB.observeSingleEvent(of: .value, with: { (snapshot) in
    for child in snapshot.children {
        print(child.key)
    }
}, withCancel: nil)

Also see:

这篇关于如何正确使用queryOrderedByValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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