Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase to load more items on scroll (Android)

I'm trying to make a chat application, I use Firebase as my backend, I'm populating my listview onCreate() with the last 10 messages sent, and I just want that when the user scrolls up ^ it will load 10 more every time.

I got all working, the problem is that the Firebase loads all the data every time, and not increasing by 10 the existing data.

For example: it loads 10 messages at startup, and when I scroll up it loads 20 messages, and then it loads 30 messages, instead of just loading 10 more at a time.

This is the code:

//Listview to find when user is scrolling and reach the top i.e firstVisibleItem == 0

messagesList.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {

        if (firstVisibleItem == 0 && !loading) {
            msgNum += 10;
            setMessagesList(msgNum); //your load more function
        }
    }
});

//Firebase to load more on scroll code:
public void setMessagesList(int msgNum) {

    MessegesRef2 = database.getReference("chatsMessages").child(chatID);
    messageValueListener = MessegesRef2.orderByChild("creationDate").limitToLast(msgNum).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            loading = true;
            for (DataSnapshot data: dataSnapshot.getChildren()) {

                ChatMessage chatMessage = data.getValue(ChatMessage.class);

                chatMessageAdapter.add(chatMessage);
                chatMessageAdapter.notifyDataSetChanged();
            }
            loading = false;
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
like image 540
sisi mendel Avatar asked Dec 21 '16 08:12

sisi mendel


People also ask

What is getKey () in Firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.

Does Firebase scale automatically?

Automatic Scaling Additionally, the API functions of firebase are designed in order to scale linearly with the size of data being synchronized. It handles the scaling operations. Your application will scale from its first user to its first million user without any change in the code.

What is the limit of Firebase realtime database?

256 MB from the REST API; 16 MB from the SDKs. The total data in each write operation should be less than 256 MB. Multi-path updates are subject to the same size limitation. The total bytes written through simultaneous write operations on the database at any given time.

Can Firebase handle large data?

Yes, it is possible to migrate to Firebase even if your database is very large. To archieve this, i suggest you watch this tutorial, The Firebase Database For SQL Developers. Even if Firebase is a NoSQL Database, i'm sure you'll be very familiar in short time.


2 Answers

I think you can use endAt()

ref.orderByChild("creationDate").limitToLast(pageSize).endAt(oldestLocalMessageTime).addValueEventListener
// pageSize is the number of messages you want to load each time (10)
// oldestLocalMessageTime is the time of oldest message that you have loaded from Firebase
// everytime you load/load more messages you need to update oldestLocalMessageTime value
like image 135
Linh Avatar answered Oct 14 '22 19:10

Linh


you can use startAt(), endAt(), and equalTo() method. When the user reaches the end of the list then call a method to load more from the server. make sure to implements ValueEventListener.

Query pagination = mDatabase.orderByChild("id").startAt(startAt).endAt(startAt + 10);
pagination.addListenerForSingleValueEvent(this);
like image 40
Milon Avatar answered Oct 14 '22 18:10

Milon