如何根据Firebase数据库中的项目计数过滤数据 [英] How to filter data based on items count in Firebase Database

查看:45
本文介绍了如何根据Firebase数据库中的项目计数过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个android聊天应用程序,并且尝试使用自定义FirebaceRecyclerAdapter在RecyrclerView中使用Firebase数据库实现无限滚动.对于第一个消息加载,到目前为止,我使用查询来从Firebase数据库获取数据,该查询按先后顺序是.orderByChild()和.limitToFirst(specificCount)来对数据进行排序.但是,当用户滚动到最后一个可见的项目时,我必须获取消息的下一部分,并且出现问题后,在获得count之后,我没有直接方法从Firebase数据库获取消息.我尝试使用该代码:

I am writing an android chat app and I am trying to implement endless scrolling using Firebase Database in RecyrclerView using custom FirebaceRecyclerAdapter. For first messages load I get data from Firebase Database by using query that ordering data by .orderByChild() and then .limitToFirst(specificCount) so far so good. But when user scrolled to the last visible item I have to get next portion of messages and the problem comes I did not get a straight way to get messages from Firebase Database after specificCount. I tried with that code:

  int startCount = mLayoutManager.getItemCount();
                FirebaseDatabase
                        .getInstance()
                        .getReference()
                        .child(RequestParameters.FB_CHILD_MESSAGES)
                        .orderByChild(RequestParameters.FB_CHILD_MESSAGES_ORDERING_TIME)
                        .startAt(startCount)
                        .endAt(startCount + CustomFirebaceRecyclerAdapter.NEXT_MESSAGE_PORTION_COUNT)
                        .addListenerForSingleValueEvent(...)

但是当我阅读有关.startAt()的文档后,我意识到这是多么愚蠢.任何人都可以帮助进行查询,以获取 specificCount 之后的项目,并将收到的邮件限制为一定数量.

But after I read the documentation for .startAt() I realize how stupid is this. Anyone could help to make a query that gets items after specificCount and limit received messages to some count.

推荐答案

我敢肯定我不到一天前就回答了这个问题,所以我建议扫描我的答案.但我会重复.

I'm pretty sure I answered this less than a day ago, so I recommend scanning my answers. But I'll repeat.

Firebase数据库API本质上不太适合分页.想一想当用户在页面1上插入一个项目(前十个项目)时会发生什么.他们最终在第2页开始时看到了上一个项目10,但再也看不到新插入的项目.

The Firebase Database API is inherently not very well suited for pagination. Think of what happens when an item is inserted while the user is on page 1 (with the first ten items). They end up seeing the previous item 10 at the start of page 2, but will never have seen the newly inserted item.

也就是说,如果您想进行分页,从技术上讲是可能的.但是,您不必告诉跳过 10个项目,而必须告诉Firebase从上一页的最后一个项目开始查询.这称为锚项,在代码中如下所示:

That said, if you want to do pagination, it is technically possible. But instead of wanting to skip 10 items, you will instead have to tell Firebase to start the query at the last item from the previous page. This is called an anchor item and in code it looks like this:

FirebaseDatabase
        .getInstance()
        .getReference()
        .child(RequestParameters.FB_CHILD_MESSAGES)
        .orderByChild(RequestParameters.FB_CHILD_MESSAGES_ORDERING_TIME)
        .startAt(timeOfLastItemOnPreviousPage, keyOfLastItemOnPreviousPage)
        .addListenerForSingleValueEvent(...)

这里的关键属性是:

  • timeOfLastItemOnPreviousPage :上一页最后一项的 FB_CHILD_MESSAGES_ORDERING_TIME 属性的值

  • timeOfLastItemOnPreviousPage: the value of the FB_CHILD_MESSAGES_ORDERING_TIME property of the last item on the previous page

keyOfLastItemOnPreviousPage ,上一页的最后一项的键(如果存在多个具有相同 FB_CHILD_MESSAGES_ORDERING_TIME 值的项,则需要此键.p>

keyOfLastItemOnPreviousPage, the key of the last item on the previous page (which is needed in case there are multiple items with the same value for FB_CHILD_MESSAGES_ORDERING_TIME

这篇关于如何根据Firebase数据库中的项目计数过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆