2

I am trying to query the most recent children in my Firebase database and then sort that limit of recent children by a child value. I understand that the Cloud Firestore has recently been released, and that querying features continue to evolve on the Firebase Realtime Database as well.

I am using the FirebaseUI FirebaseRecyclerAdapter, and within that adapter you must enter a query. I am tweaking to add 2 queries to my RecyclerView. I am doing so as follows:

mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(
    Poll.class, R.layout.latest_item, PollHolder.class, 
    mBaseRef.child("Polls").limitToLast(3).orderByChild("vote_count"))

Unfortunately, it is not working, as it is first ordering the whole database by the child value and then displaying those 5 values. Essentially, the result is flipped in terms of what I am seeking: I want to query by limiting to the most recent X children and THEN sort those by child value. Any ideas where I am going wrong?

Edit:

Here is my Firebase Database. Essentially, I want to limit the query to the last 3 items in that database, and then sort those 3 items by vote count. I then want them to populate the RecyclerView accordingly. I am trying to do that in the code snippet listed above, but instead of actually limiting to the last 3 items in the database it is querying the whole database.

enter image description here

tccpg288
  • 3,242
  • 5
  • 35
  • 80
  • I'm having a hard time understanding your requirement. Are you looking to load the items with the highest vote count? (it might help if you post a snippet of the JSON you're querying) – Frank van Puffelen Feb 03 '17 at 04:33
  • Frank, I have included the database and additional detail on what I am trying to do. Thanks again. – tccpg288 Feb 06 '17 at 01:43
  • Ah OK, I understand now. It's not possible to get the latest three items (which requires ordering by key) in order of their score (a child property). A query can only order/filter on one property. But sorting the latest three items in a list on the score shouldn't be difficult to do client-side. – Frank van Puffelen Feb 06 '17 at 03:53
  • Thanks Frank, does this mean that under the current setup I cannot generate a recyclerview that limits and sorts? It has to be one or the other? – tccpg288 Feb 06 '17 at 04:01
  • Yes. See some of these: http://stackoverflow.com/search?q=%5Bfirebase-database%5D+multiple+conditions – Frank van Puffelen Feb 06 '17 at 04:06
  • Thanks again. So it doesn't seem possible, any plans to add this type of query capability? – tccpg288 Feb 06 '17 at 04:11
  • Hi Frank - Just wanted to reach out and see if this option is yet available. I am using the latest Firebase library 10.2.1. I want to populate a RecyclerView using the FirebaseRecyclerAdapter, first limiting to recent items in my Database (.limitToLast) and then sorting those recent items (.orderByChild). – tccpg288 Apr 26 '17 at 02:17
  • Hi Frank - with the announcement of Cloud Firestore I wanted to reach out on this question again. I have also read about the third-party library querybase. Essentially, I want to first limit the results displayed in the RecyclerView to a set number of the latest items added to Firebase, to capture the most recent items. Then, after I have captured the most recent items, I want to sort by a child (vote count). Thanks again! – tccpg288 Oct 04 '17 at 01:22
  • With Cloud Firestore (and the Firebase SDKs for it) you can order by multiple properties. That might be enough to implement your use-case. If you're having problems with it, post a new question with the [minimal code that reproduces where you're stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Oct 04 '17 at 03:38
  • Thanks Frank, is this a new feature? I remember it was previously not possible .orderBy() and .limitToLast() simultaneously. – tccpg288 Oct 04 '17 at 12:14
  • Cloud Firestore is a new product, launched yesterday. :-) Note that it's a separate product though, it's not a new feature of the Firebase Realtime Database. Queries on the latter are still limited to a single property. For your options there, see http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Oct 04 '17 at 13:47
  • Got it - I was reading the documentation and it mentioned complex querying. I wasn't sure if that encompassed what I was trying to accomplish. Thanks again. – tccpg288 Oct 04 '17 at 14:07

0 Answers0