0

I'm trying to query by the "genre": "Arts & Humanities" but also need the queryEnding to paginate my data based on the timestamp.

However, we can't use both of them combined in Xcode. What's another method?

Here's what I'm trying: (not working)

queryRef = ref.queryOrdered(byChild: "genre").queryEqual(toValue: "Arts & Humanities").queryEnding(atValue: lastTimestamp)

Here's the code I use to paginate my data. Works fine, but I can't sort the pagination with data only for "Arts & Humanities". this will paginate my entire feed.

        var queryRef:DatabaseQuery
        let lastPost = self.postList.last


        if lastPost != nil {
            let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000

            queryRef = ref.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)

        } else {
            queryRef = ref.queryOrdered(byChild: "timestamp")

        }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

2 Answers2

0

You can use queryOrdered with queryEqual:

queryRef = ref.queryOrdered(byChild : "genre").queryEqual(toValue : "Arts & Humanities")

Then use the observe method to retrieve data, this will retrieve the data that has genre = Arts & Humanities, then inside the listener create another query, this time you only use the queryEnding method and attach another listener to retrieve the data according to the query.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

There is no way to use two properties in a query. For a full explanation of why that is, and the limited options to work around it, see my answer here: Query based on multiple where clauses in Firebase

But in your case, you might be able to get by in a simpler way. If there are multiple nodes with the same genre, you can pass the key as a second argument to queryStarting(atValue:) and queryEnding(atValue:). So instead of trying to add an additional filter in the timestamp, use the key to disambiguate with something like:

queryRef = ref.queryOrdered(byChild: "genre").queryStarting(atValue: "Arts & Humanities", keyOfLastItemOnPreviousPage)

Where keyOfLastItemOnPreviousPage is the key of the last item on the previous page. This will then also become the first item on the next page, so you'll want to retrieve one more child node than you need, and skip that client-side if you don't want to show overlap.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807