0

Im making query and trying to make an order by rating field. This is paginated list so im making partially queries.

But my query is not sorted.

   private void callRealOnlineUsersList(int page, String neededGender) {
    DatabaseReference mReference = realtimeReference.child("OnlineUsers/" + neededGender);
    Query query = mReference.limitToLast(page * TOTAL_ITEMS_TO_LOAD);
    query.orderByChild("rating");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            final List<OnlineUser> userList = new ArrayList<>();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                OnlineUser user = postSnapshot.getValue(OnlineUser.class);
                userList.add(new OnlineUser(user.getUid(), user.getName(), user.getImage(), user.getGender(), user.getCountry(), user.getRating()));
            }
            EventBus.getDefault().post(new ListEvent(userList));
        }

Logs are

 D/testList: rating 2
            D/testList: rating 1
            D/testList: rating 25
            D/testList: rating 3
            D/testList: rating 1
            D/testList: rating 4
            D/testList: rating 1
            D/testList: rating 10
            D/testList: rating 2
            D/testList: rating 1
            D/testList: rating 25

Datasnapshot enter image description here

Bo Z
  • 2,359
  • 1
  • 13
  • 31
  • Does this answer your question? [Firebase Data Desc Sorting in Android](https://stackoverflow.com/questions/34156996/firebase-data-desc-sorting-in-android) – robsiemb Nov 29 '19 at 17:06
  • @robsiemb why my code not working even with ascending order? – Bo Z Nov 29 '19 at 17:09
  • Ah, yes, I see your problem (but you still can't sort by descending values) – robsiemb Nov 29 '19 at 17:18

1 Answers1

2

So, you've combined 3 questions into 1:

  • Why isn't my code sorting at all?
  • Why can't I sort in descending order?
  • Given a query that works, how should I do pagination with Firebase RTDB on Android?

Good news, the later 2 of these have good existing answers on StackOverflow!

First, the way you're using Query here:

Query query = mReference.limitToLast(page * TOTAL_ITEMS_TO_LOAD);
query.orderByChild("rating");

isn't correct (and this is why you aren't getting sorted results at all).

Instead try this:

Query query = mReference
                 .limitToLast(page * TOTAL_ITEMS_TO_LOAD)
                 .orderByChild("rating");

If you just call orderByChild, it returns a query that does that, but in your code you are ignoring the result.

To help make it clearer what is happening, you could also do this and it would work (but your current code doesn't save the result of the orderByChild call:

Query query = mReference.limitToLast(page * TOTAL_ITEMS_TO_LOAD);
query = query.orderByChild("rating");

Second, you can't sort by descending order in Firebase RTDB, so you should look at that question for possible solutions.

Finally, your current code will always be returning larger and larger numbers of results as page increases -- you don't always get just TOTAL_ITEMS_TO_LOAD results. A more complete SO question about pagination is here.

robsiemb
  • 6,157
  • 7
  • 32
  • 46
  • what do you mean ignoring? could you please explain? – Bo Z Nov 29 '19 at 17:21
  • You're not using the return value. It just gets thrown away, and then you pass a query which only has a limit to the `ValueEventListener`. – robsiemb Nov 29 '19 at 17:23
  • could you please edited code how it suppose to be to understand my issue? – Bo Z Nov 29 '19 at 17:26
  • I don't quite understand what you are asking. I've added your incorrect code to my answer, that code needs to be replaced with one of the two options I provided. – robsiemb Nov 29 '19 at 17:28
  • I have tried your code but now it takes only first ten items and not getting rest of them – Bo Z Nov 29 '19 at 17:33
  • I presume `TOTAL_ITEMS_TO_LOAD` is 10? That's exactly what you are asking for. – robsiemb Nov 29 '19 at 17:35
  • yes i need to make 10 + next 10 + next 10... but for some reason it is not working – Bo Z Nov 29 '19 at 17:36
  • There's also good existing questions for pagination, rather than writing duplicate code, I've added one to my answer. – robsiemb Nov 29 '19 at 17:36
  • could you please let me know the code where we call listener to query? – Bo Z Nov 29 '19 at 17:46
  • You mean this part of your code? `query.addValueEventListener(new ValueEventListener() { ... }` ? – robsiemb Nov 29 '19 at 17:47
  • yes is it suppose to be after `Query query = mReference .limitToLast(page * TOTAL_ITEMS_TO_LOAD) .orderByChild("rating");` ??? – Bo Z Nov 29 '19 at 17:47
  • It would have to be, otherwise there's no query to listen to. – robsiemb Nov 29 '19 at 17:48
  • but how i can get a snapshot which returns after query been made? – Bo Z Nov 29 '19 at 17:49
  • The snapshot is the value that is passed to the `onDataChange` method of the `ValueEventListener`. You're asking a large number of questions in sequence. If you have additional questions (beyond the 3 answered above), it probably makes sense to ask new, specific, stackoverflow questions rather than continue an extended comments discussion. – robsiemb Nov 29 '19 at 17:51
  • hmm... im sorry if it looks like that... but im just trying to clarify answer you gave. to get the whole picture not you provided some info but one part is absent thats why im asking – Bo Z Nov 29 '19 at 17:52