Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

swift - Querying below AutoID's in Firebase

Results
- auto generated ID
  - auto generated ID
       value1: abc
       value2: def

I want to be able to query where "value2" is equal to some specific value. I start with a reference:

let ref = FIRDatabase.database().reference().child("Results")

Since I don't know the auto generated ID's in order to supply a path do I need to get them first in order to get down to value where I can use .queryEqualToValue? If so am I not just grabbing every record to look for a value versus using some kind of index to grab only those where value2 is equal to a value?

In the end I would like a query that returns all "records" where value2 is equal to a certain value. It feels like I need to iterate through every record to do that. I feel like I am missing something here.

Update:

I've tried:

self.ref.queryOrderedByKey().queryEqual(toValue: "def", childKey: "value2").observeSingleEvent(of: .value, with: {(snapshot) in
            print(snapshot) })

This crashes with an error I give in comments below.

I've tried:

self.ref.queryOrdered(byChild: "value2").queryEqual(toValue: "def").observeSingleEvent(of: .childAdded, with: {(snapshot) in

This never executes the closure. If I change the observer to ".value"it returns null.

If I do this:

self.ref.queryOrdered(byChild: "value2").observeSingleEvent(of: .childAdded, with: {(snapshot) in

It will actually sort the data properly. It won't sort properly with ".value" Regardless, adding queryEqual then doesn't work as described above.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...