iOS - Firebase过滤器查询 [英] iOS - Firebase Filter query
问题描述
$ b在分数节点下,我的数据结构如下:
{
的jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-Kak6FCyGtdy_kcEPd4K= {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = -Kak6FCyGtdy_kcEPd4K;
分数= 5;
};
的jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-KbE_pgfUsukOm4uW0bx= {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = -KbE_pgfUsukOm4uW0bx;
分数= 4;
};
问题: 我尝试过: 't得到结果? < img src =https://i.stack.imgur.com/lvniL.pngalt =图片1> 当您调用 解决方法是使用 I got score node in Firebase: And under score node my data structure is as: Question:
Can I filter the data by score ? I tried doing : But can't get the result ? When you execute a query against Firebase, you get a When you're calling The solution is to use the
这篇关于iOS - Firebase过滤器查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
可否过滤数据
pre $ 在
中调用observeSingleEvent(of:.value,){debug())debugPrint() snapshot.valuenil)
})
FIRDataSnapshot
,其中包含每条结果的三条信息:
snapshot.value
快照热被转换成一个字典。不幸的是,字典只能保存键值对,所以你丢失了关于节点位置的信息。所以虽然快照中的数据实际上是正确排序的,但是您将丢弃这些信息。
FIRDataSnapshot
s内置 children
属性以正确的顺序遍历子节点。有关查询的 Firebase文档包含此示例那么:
$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $快照$
...
}
})
{
"jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-Kak6FCyGtdy_kcEPd4K" = {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = "-Kak6FCyGtdy_kcEPd4K";
score = 5;
};
"jjTO29ziQ2SjrH5PFzY7ZMpSgjq1-KbE_pgfUsukOm4uW0bx" = {
jjTO29ziQ2SjrH5PFzY7ZMpSgjq1 = "-KbE_pgfUsukOm4uW0bx";
score = 4;
};
FIRDatabase.database().reference().child("scores").queryOrdered(byChild: "score").observeSingleEvent(of: .value, with: { (snapshot) in
debugPrint(snapshot.value ?? "nil")
})
FIRDataSnapshot
that contains three pieces of information for each result:
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. FIRDataSnapshot
s 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 {
...
}
})