简体   繁体   English

Firebase分页limitToFirst()与limitToLast()

[英]Firebase pagination limitToFirst() vs limitToLast()

I was trying to implement Firebase in android, I have few queries in that, I would be glad if you would help me into this. 我正在尝试在android中实现Firebase,在那我几乎没有查询,如果您能帮助我,我将非常高兴。 Getting bit confused in it. 变得有点困惑。 So here is my question. 所以这是我的问题。

I am showing Firebase chat in listview in which I want to only load 1st 100 messages from firebase after that User will do pull-to-refresh then load 1st 200 messages and so on. 我正在listview中显示Firebase聊天,在该listview中,我只希望从firebase加载第1条100条消息,然后用户将进行“按需pull-to-refresh然后加载第1条200条消息,依此类推。

在此处输入图片说明

For that I am looking in limitToFirst() and limitToLast() not getting idea which one is better. 为此,我正在寻找limitToFirst()limitToLast()不知道哪个更好。

Can anyone please help. 谁能帮忙。

I did almost same thing in this way: 我以这种方式做了几乎相同的事情:

In your class (I assume that is an Activity), create a member variable of Query type: 在您的类中(我假设这是一个Activity),创建Query类型的成员变量:

private Query queryChat;

In the onStart method, start doing the query: onStart方法中,开始执行查询:

@Override
public void onStart() {
    super.onStart();

    // initialize your value event listener

    queryChat= mFirebaseDatabaseReference.
            .orderByChild("timestamp")
            .limitToLast(pageSize);
    queryChat.addListenerForSingleValueEvent(yourValueEventListener);

}

pageSize is an int variable and here has to be set to 100. pageSize是一个int变量,此处必须设置为100。

Doing in this way, you are first making an ascending order of your messages (so, the first messages in the resulting ordered list are the oldest), and then you are picking the last pageSize messages (so, the pageSize last in order of time). 这样,您首先要对消息进行升序排列(因此,结果排序列表中的第一条消息是最早的),然后选择最后的pageSize消息(因此,按时间顺序将pageSize在最后) )。 So, you will get the first (in order of time, starting from the newest) pageSize messages. 因此,您将获得第一(按时间顺序,从最新的开始) pageSize消息。

I'm supposing that you already have implemented a ValueEventListener here. 我想您已经在这里实现了ValueEventListener Also, timestamp is a field in firebase message in which you stored the Timestamp in milliseconds of your message. 另外, timestamp是Firebase消息中的一个字段,您在其中存储了以毫秒为单位的时间戳。

Now, define a method in your class like this: 现在,在您的类中定义一个方法,如下所示:

private void loadMoreItems() {

    queryChat= mFirebaseDatabaseReference.
            .orderByChild("timestamp")
            .endAt(messageList.get(messageList.size()-1).getTimestamp())
            .limitToLast(pageSize);
    queryChat.addListenerForSingleValueEvent(yourValueEventListener);
}

The messageList here is an ArrayList that stores the chat messages, and is filled inside the ValueEventListener, when you get the messages from firebase. 这里的messageList是一个存储聊天消息的ArrayList,当您从firebase获取消息时,该列表将填充在ValueEventListener中。

Here, I'm telling to firebase to do the ascending order (as before), but picking only those items with timestamp <= the last item already visible to the user.( .endAt(messageList.get(messageList.size()-1).getTimestamp()) is telling to firebase exactly this), and finally limitToLast as before. 在这里,我要告诉firebase进行升序(如前所述),但只选择那些时间戳<=对用户已经可见的最后一个项目的项目。(. .endAt(messageList.get(messageList.size()-1).getTimestamp())正好告诉火力limitToLast ),最后像以前一样limitToLast

And now you can call this method ( loadMoreItems ) whenever you want (in your case, when the user do pull to refresh). 现在,您可以随时调用此方法( loadMoreItems )(在您的情况下,当用户确实要刷新时)。

You can try this way: 您可以这样尝试:

int mPageEndOffset = 0;
int mPageLimit = 10;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    connectDetector = new ConnectDetector(getActivity());
    if(connectDetector.getConnection()) {

        alanRef = new Firebase(firebaseURL).child(EventChatActivity.SuperCateName + "/ " + eventDetail.getCategory_name() + "/ " + eventDetail.getEvent_title() + "/ " + EventChatActivity.eventID).child("StadiumChat");
        alanQuery = alanRef.limitToFirst(mPageLimit);
        userName = MyApp.preferences.getString(MyApp.USER_NAME, null);

        EventChatActivity.eventID).child("StadiumChat");
    }
}    

This will fetch records of latest 10 messages from firebase. 这将从Firebase获取最新的10条消息的记录。

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

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