简体   繁体   English

如何使用分页在 Firebase 中实时降序?

[英]How to descending order in Firebase realtime with pagination?

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数据快照在此处输入图片说明

So, you've combined 3 questions into 1:因此,您已将 3 个问题合并为 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?给定一个有效的查询,我应该如何在 Android 上使用 Firebase RTDB 进行分页?

Good news, the later 2 of these have good existing answers on StackOverflow!好消息,后两个在 StackOverflow 上有很好的现有答案!

First, the way you're using Query here:首先,您在此处使用Query的方式:

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.如果您只是调用orderByChild ,它会返回一个执行此操作的查询,但在您的代码中您忽略了结果。

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:为了更清楚地说明发生了什么,您也可以这样做并且它会起作用(但您当前的代码不保存orderByChild调用的结果:

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.其次,您无法在 Firebase RTDB 中按降序排序,因此您应该查看该问题以寻找可能的解决方案。

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.最后,随着page增加,您当前的代码将始终返回越来越多的结果——您并不总是只得到TOTAL_ITEMS_TO_LOAD结果。 A more complete SO question about pagination is here .关于分页的更完整的 SO 问题在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM