-1

I'm making an iOS which integrate Firebase SDK. I got a problem about query data on it.

For example, I have a JSON data on Firebase

Source from 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/

Can I query some dinosaurs which height satisfies following condition:

height * height + length * length > 100 (just for example)

If possible, how can I do this using iOS Firebase SDK?

Reference about querying data using iOS Firebase SDK https://www.firebase.com/blog/2014-11-04-firebase-realtime-queries.html

Scofield Tran
  • 828
  • 1
  • 18
  • 30
  • 1
    Can you please update your answer with a copy/paste of your actual Firebase data structure instead of a picture? You should check out the Export Data button in the upper right corner of your Firebase dashboard to make it easier. The actual data helps us, help you, since we dont have to re-type everything. – Jay Mar 03 '16 at 18:22
  • Actually, I've create app on Firebase, but I wonder if it can solve my problem first so I want to make sure first – Scofield Tran Mar 04 '16 at 08:56

1 Answers1

2

(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:

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

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.

Oh, and see this post

Filter and sort on multiple values with Firebase


For the equation posted in the original question: height * 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.

Say baseHeight = 45 or baseHeight = (100-10) / 2 or 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

maxHeight = baseHeight + 10

ref.queryOrderedByKey().queryStartingAtValue(baseHeight).queryEndingAtValue(maxHeight)
   .observeEventType(.ChildAdded, withBlock: { snapshot in
    println(snapshot.key)
})
Community
  • 1
  • 1
Jay
  • 34,438
  • 18
  • 52
  • 81
  • Thank @Jay, your solution is great, but I've updated my question with new more complex example condition. This condition contains 2 fields and they gather together into one condition, is it still possible' – Scofield Tran Mar 04 '16 at 08:53
  • Since the answer provided was correct, you should have accepted it, then created a new question since the parameters changed, making it a different question. – Jay Mar 04 '16 at 18:12
  • Thank @Jay, I accepted your answer, could you answer new one, If correct, I'll accept your new answer, too – Scofield Tran Mar 05 '16 at 03:30
  • Sorry, I saw your new answer – Scofield Tran Mar 05 '16 at 03:31