Firebase queryOrderedByChild() 方法未提供排序数据 [英] Firebase queryOrderedByChild() method not giving sorted data

查看:13
本文介绍了Firebase queryOrderedByChild() 方法未提供排序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库结构是这样的:

<代码>{用户":{爱情":{"name": "Ada Lovelace",分数":4,},"ghopper": { ... },eclarke":{ ... }}}

我正在尝试按降序检索前 20 个分数.

让 queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToLast(20)queryRef.observeSingleEventOfType(.Value, withBlock: { (querySnapShot) in打印(querySnapShot.value)})

我正在尝试获得类似的输出

score": 4得分":3得分":2或者得分":2得分":3得分":4或者234

请告诉我如何解决这个问题.

解决方案

使用方法 observeEventType 而不是 observeSingleEventOfType.此外,将 FIRDataEventType 设为 ChildAdded.

最后,如果您想要前 20 项,请使用 queryLimitedToFirst 而不是 queryLimitedToLast.

<代码>{用户":{爱情":{"name" : "Ada Lovelace",分数":4},埃克拉克":{"name" : "艾米丽·克拉克",分数":5},地鼠":{"name" : "格蕾丝·霍珀",分数":2}}}

对于上面的数据集

让 queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in打印(键:(快照.键),值:(快照.值)")})

<块引用>

键:ghopper,值:可选({名称 = 格蕾丝料斗;分数 = 2;})

key: alovelace, value: Optional({姓名 = 艾达·洛夫莱斯;分数 = 4;})

键:eclarke,值:可选({姓名 = 艾米丽·克拉克;分数 = 5;})

Snapshot 会将内容作为本机类型返回.返回的数据类型:

  • NSDictionary
  • NSArray
  • NSNumber(也包括布尔值)
  • NSString

所以,您可以通过这种方式获得分数.

 let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in如果让分数 = snapshot.value 作为?NSD字典{打印(分数[分数"])}})

<块引用>

可选(2)

可选(4)

可选(5)

此外,实时数据库默认以升序返回所有内容.

如果你想要降序,你可以做一些技巧(4:40) 在您的数据库中.

My database structure is some thing like this:

{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      "score": 4,
    },
    "ghopper": { ... },
    "eclarke": { ... }
  }
}

I am trying to retrieve top 20 scores in descending order.

let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToLast(20)
queryRef.observeSingleEventOfType(.Value, withBlock: { (querySnapShot) in
      print(querySnapShot.value)
})

i am trying to get output like

score": 4
score": 3
score": 2

or 

score": 2
score": 3
score": 4

or 

2
3
4

Please let me know how to solve this.

解决方案

Use method observeEventType instead of observeSingleEventOfType. Also, make FIRDataEventType to ChildAdded.

Last, If you want Top 20 items, use queryLimitedToFirst instead of queryLimitedToLast.

{
  "users" : {
    "alovelace" : {
      "name" : "Ada Lovelace",
      "score" : 4
    },
    "eclarke" : {
      "name" : "Emily Clarke",
      "score" : 5
    },
    "ghopper" : {
      "name" : "Grace Hopper",
      "score" : 2
    }
  }
}

For the dataset above

let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)
queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
    print("key: (snapshot.key), value: (snapshot.value)")
})

key: ghopper, value: Optional({ name = Grace Hopper; score = 2; })

key: alovelace, value: Optional({ name = Ada Lovelace; score = 4; })

key: eclarke, value: Optional({ name = Emily Clarke; score = 5; })

Snapshot will returns the contents as native types. Data types returned:

  • NSDictionary
  • NSArray
  • NSNumber (also includes booleans)
  • NSString

So, you can get your scores this way.

    let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)
queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
    if let scores = snapshot.value as? NSDictionary {
        print(scores["score"])
    }
})

Optional(2)

Optional(4)

Optional(5)

Moreover, the default of realtime database return everything in ascending order.

If you want descending order, you can make some tricks(4:40) in your database.

这篇关于Firebase queryOrderedByChild() 方法未提供排序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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