本文介绍了Firebase queryOrderedByChild()方法不给出排序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好,我的数据库结构是这样的 {users:{ alovelace:{name:Ada Lovelace,score:4,},ghopper:{...},eclarke:{...} } } 我试图以降序的方式检索前20个分数。 let queryRef = FIRDatabase.database()。reference()。child (users)。queryOrderedByChild(score)。queryLimitedToLast(20) queryRef.observeSingleEventOfType(.Value,withBlock:{(quearySnapShot)in print(quearySnapShot.value)} ) 我正在试图获得输出,例如 得分:4 得分:3 得分:2 或 得分 :2 得分:3 得分:4 或 2 3 4 请让我知道如何解决这个问题。 使用方法 observeEventType 而不是 observeSingleEventOfType 。 另外,使FIRDataEventType为 ChildAdded 。 $ b $ p 最后,如果您想要Top 20项目,请使用 queryLimitedToFirst 而不是 queryLimitedToLast $ b $ {用户:{alovelace:{name:Ada Lovelace,得分:4 },eclarke:{name:Emily Clarke,得分:5 },ghopper:{name:Grace Hopper,score:2 } 对于上面的数据集 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:可选({ name = Grace Hopper; score = 2; }) 键:alovelace,值:可选({ name = Ada Lovelace; 分数= 4; key:eclarke,value:可选({$ b $ name = Emily Clarke; score = 5; ) 快照将以本机类型返回内容。 返回的数据类型: NSDictionary NSArray NSNumber(也包括布尔值) 所以,你可以得到你的分数是这样的。 $ b $ pre $ let queryRef = FIRDatabase.database()。reference()。child(users)。queryOrderedByChild (score)。queryLimitedToFirst(20) queryRef.observeEventType(.ChildAdded,withBlock:{(snapshot)in 如果let scores = snapshot.value as?NSDictionary { print(scores [score])} }) 可选(2) 可选(4) 可选(5) $此外,实时数据库的默认值以升序返回所有内容。 $ b 如果您想降序,您可以制作一些 技巧 (4:40)。 Hi 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: { (quearySnapShot) in print(quearySnapShot.value)})i am trying to get output likescore": 4score": 3score": 2or score": 2score": 3score": 4or 234Please 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 abovelet 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:NSDictionaryNSArrayNSNumber (also includes booleans)NSStringSo, 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()方法不给出排序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 07:27