简体   繁体   English

在iOS Firebase SDK上查询条件复杂的数据

[英]Query data with complex condition on iOS Firebase SDK

I'm making an iOS which integrate Firebase SDK. 我正在制作一个集成Firebase SDK的iOS。 I got a problem about query data on it. 我对它的查询数据有疑问。

For example, I have a JSON data on Firebase 例如,我在Firebase上有一个JSON数据

Source from https://www.firebase.com/blog/2014-11-04-firebase-realtime-queries.html 来源自https://www.firebase.com/blog/2014-11-04-firebase-realtime-queries.html

{
  "bruhathkayosaurus": {
    "appeared": -70000000,
    "height": 25,
    "length": 44,
    "order": "saurischia",
    "vanished": -70000000,
    "weight": 135000
  },
  "lambeosaurus": {
    "appeared": -76000000,
    "height": 2.1,
    "length": 12.5,
    "order": "ornithischia",
    "vanished": -75000000,
    "weight": 5000
  },
  ...

Data from https://dinosaur-facts.firebaseio.com/ 来自https://dinosaur-facts.firebaseio.com/的数据

Can I query some dinosaurs which height satisfies following condition: 我可以查询一些满足以下条件的恐龙吗:

height * height + length * length > 100 (just for example) 高度*高度+长度*长度> 100(仅作为示例)

If possible, how can I do this using iOS Firebase SDK? 如果可能,如何使用iOS Firebase SDK执行此操作?

Reference about querying data using iOS Firebase SDK https://www.firebase.com/blog/2014-11-04-firebase-realtime-queries.html 有关使用iOS Firebase SDK查询数据的参考https://www.firebase.com/blog/2014-11-04-firebase-realtime-queries.html

(note that the question changed so the answer I provided previously which was correct is no longer valid for the new question) (请注意,问题已更改,因此我之前提供的正确答案不再适用于新问题)

The new answer is no, you cannot directly query for two values at the same time. 新的答案是否定的,您不能同时直接查询两个值。

However, there are ways around that and since you are using iOS, it's a snap: 但是,有多种解决方法,并且由于您使用的是iOS,因此很简单:

1) query all dinosaurs to load them into objects and store those in an array 2) craft an NSPredicate to pull out an array based on your equations 1)查询所有恐龙以将它们加载到对象中并将它们存储在数组中2)制作NSPredicate根据您的方程式提取数组

You should really think about how you model your data within firebase because if you modal it correctly, you would be able to query Firebase for what you want instead of handling it client side. 您应该真正考虑如何在firebase中建模数据,因为如果正确地对其进行模式化,则可以查询Firebase以获取所需内容,而无需在客户端进行处理。

Oh, and see this post 哦,看看这篇文章

Filter and sort on multiple values with Firebase 使用Firebase筛选和排序多个值


For the equation posted in the original question: height * 2 + 10 > 100 对于原始问题中发布的方程式:高度* 2 + 10> 100

The answer is yes, you can do that 答案是可以的。

Define a height, either with a static number or via an equation and then use that in the .queryStartingAtValue parameter. 使用静态数字或通过方程式定义高度,然后在.queryStartingAtValue参数中使用该高度。

Say baseHeight = 45 or baseHeight = (100-10) / 2 or baseHeight = 2^5 假设baseHeight = 45或baseHeight =(100-10)/ 2或baseHeight = 2 ^ 5

ref.queryOrderedByChild("height").queryStartingAtValue(baseHeight)
   .observeEventType(.ChildAdded, withBlock: { snapshot in
    println(snapshot.key)
})

You can further narrow your results by adding .queryEndingAtValue, which will then give you a range of heights 您可以通过添加.queryEndingAtValue来进一步缩小结果范围,这将为您提供一定范围的高度

maxHeight = baseHeight + 10

ref.queryOrderedByKey().queryStartingAtValue(baseHeight).queryEndingAtValue(maxHeight)
   .observeEventType(.ChildAdded, withBlock: { snapshot in
    println(snapshot.key)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM