iOS - Firebase过滤器查询 [英] iOS - Firebase Filter query

查看:123
本文介绍了iOS - Firebase过滤器查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中得到了分数节点:$ b​​
$ b在分数节点下,我的数据结构如下:

  {
的jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-Kak6FCyGtdy_kcEPd4K= {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = -Kak6FCyGtdy_kcEPd4K;
分数= 5;
};
的jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-KbE_pgfUsukOm4uW0bx= {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = -KbE_pgfUsukOm4uW0bx;
分数= 4;
};

问题:
可否过滤数据

我尝试过:

pre $

中调用observeSingleEvent(of:.value,){debug())debugPrint() snapshot.valuenil)

})

't得到结果?



< img src =https://i.stack.imgur.com/lvniL.pngalt =图片1>

解决方案

当您针对Firebase执行查询时,您会得到一个 FIRDataSnapshot ,其中包含每条结果的三条信息:


  • 其键

  • 其值

  • 相对于其他节点的位置


当您调用 snapshot.value 快照热被转换成一个字典。不幸的是,字典只能保存键值对,所以你丢失了关于节点位置的信息。所以虽然快照中的数据实际上是正确排序的,但是您将丢弃这些信息。

解决方法是使用 FIRDataSnapshot s内置 children 属性以正确的顺序遍历子节点。有关查询的 Firebase文档包含此示例那么:

$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $快照$
...
}
})


I got score node in Firebase:

And under score node my data structure is as:

{
"jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-Kak6FCyGtdy_kcEPd4K" =     {
    jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = "-Kak6FCyGtdy_kcEPd4K";
    score = 5;
};
"jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-KbE_pgfUsukOm4uW0bx" =     {
    jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = "-KbE_pgfUsukOm4uW0bx";
    score = 4;
};

Question: Can I filter the data by score ?

I tried doing :

FIRDatabase.database().reference().child("scores").queryOrdered(byChild: "score").observeSingleEvent(of: .value, with: { (snapshot) in

        debugPrint(snapshot.value ?? "nil")

    })

But can't get the result ?

解决方案

When you execute a query against Firebase, you get a FIRDataSnapshot that contains three pieces of information for each result:

  • its key
  • its value
  • its position relative to other nodes

When you're calling snapshot.value the snapshot is converted into a Dictionary. Unfortunately that dictionary can only hold key-value pairs, so you lose the information on the position of the nodes. So while the data in the snapshot is actually sorted correctly, you're throwing that information away.

The solution is to use the FIRDataSnapshots built-in children property to loop over the child nodes in the correct order. The Firebase documentation on querying has this sample of how to do that:

_commentsRef.observeEventType(.Value, withBlock: { snapshot in
  for child in snapshot.children {
    ...
  }
})

这篇关于iOS - Firebase过滤器查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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