2

I am trying to write query to firebase that get posts by posterId and for specified date range.
Dates are stored as number.

reference.child("posts")
                .queryOrderedByChild(posterId)
                .queryStartingAtValue(1483706035, childKey: "postedOnDate")
                .queryEndingAtValue(1483707046)
                .queryEqualToValue("-KasdASDHGASasdsa986")
                .observeEventType(...

I am getting error:

'InvalidQueryParameter', reason: 'Can't call queryEqualToValue: after queryStartingAtValue, queryEndingAtValue or queryEqualToValue was previously called'

If I remove queryEqualToValue I don't get error but I don't get data neither.
What I did wrong in my query, or what I am missing in this query?
It's simple query don't know why it doesn't work.

1110
  • 7,829
  • 55
  • 176
  • 334
  • Firebase Database queries can only order/filter on one property/key at a time. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase. The `childKey:` parameter does not allow you to pass in a second property name, but is instead used in pagination scenarios. See http://stackoverflow.com/questions/41065931/filtered-paginated-list-with-firebase and http://stackoverflow.com/questions/38933148/firebase-queryorderedbychild-along-with-equaltovalue-and-startingatvalue-combina/38934094#38934094 – Frank van Puffelen Jan 06 '17 at 15:40

2 Answers2

0

please try this code,it helps you

ref.child("posts")
            .queryOrderedByChild(posterId)
            .queryStartingAtValue("Range1")
            .queryEndingAtValue("Range2")
                   .observeEventType(.Value, withBlock: { snapshot in
                    print(snapshot.key)
                })
sunil Kumawat
  • 475
  • 3
  • 14
0

Here is how I am creating a date data, uploading it to firebase, retrieving and then finally, sorting it. All done based on the nearest date in my chat JSON tree

This is the end result for retrieving keys from my firebase database based on the nearest date


enter image description here


First declare a constant called timestamp

 let timestamp = NSNumber(value: Int(Date().timeIntervalSince1970))

Synchronized it with the other values in your DB, and push it to firebase

enter image description here

finally, the comparison method

    // DISPAY AND SORT TIMER BETWEEN CHAT MESSAGES BASED ON TIMESTAMP
    self.messages.sort(by: { (message1, message2) -> Bool in

        (message1.timestamp.intValue)! > (message2.timestamp?.intValue)!

    })
shree bhagwat
  • 392
  • 2
  • 11
Khaledonia
  • 2,054
  • 3
  • 15
  • 33