0

I have a recyclerview. In that I'm displaying data from Firebase Realtime DB. It is a grocery App. I have Fruits, vegetables and Provisional Items. I have used (My firebase path.... child(..).orderbyChild("Category").equalto("Fruits").

Upto this, it's working fine. I'm getting Fruits alone.

Next thing is, I need to keep all the not in stock products at the bottom of my recycler view. If I add one more orderbyChild("NotInStock"), the app is crashing.

Is there any way to solve this or alternate way to achieve this.. Need some help here.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Some of the code would be hepful. How/where do you store the data and how do you propagate them to the adapter. – TheLibrarian Oct 25 '22 at 13:10
  • Also, it is not clear what you expect to happen. A detailed example would be very helpful – CarlesCF Oct 25 '22 at 13:15
  • 1
    Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For example, you could create `"Category_NotInStock": "Fruits_True"` and filter on that. For a longer example of this and other approaches, see my answer here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Oct 25 '22 at 13:25

1 Answers1

1

Per documentation of #orderByChild:

Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query is an error.

This is further emphasized in the "Sorting Data" section under their Lists of Data page:

You can only use one order-by method at a time. Calling an order-by method multiple times in the same query throws an error.

In a typical SQL environment, it'd be a simple ORDER BY NotInStock ASC, Category. As far as alternative approaches (bearing in mind firebase's limitations):

  1. You can query the relevant data twice, first for in-stock items, then again for out-of-stock once you've exhausted the "in-stock" data.

  2. You can query all of the data at once, and then sort/categorize the data further once you have retrieved it from firebase.

  3. You can follow further additional approaches outlined in the above linked duplicate question: Query based on multiple where clauses in Firebase

Rogue
  • 11,105
  • 5
  • 45
  • 71