I want to load only 10 posts at first, then the rest

256 views
Skip to first unread message

Aijaz & Sarfaraz

unread,
Jul 16, 2020, 4:59:10 PM7/16/20
to Firebase Google Group
Hello All,
 
We are newbies ( Aijaz 18 years  and me, Sarfaraz, 17 years ) and are learning android app development via watching Youtube video series.

Very recently we have complete watching INSTAGRAM App with Firebase - Android Studio Tutorial and have followed all the videos and it works fine :).

We have also shared with our friends and are using this app.

However, there is an issue with the app. App have become slow when it loads the posts of users.

Initially it was fine but later when our friends started posting now it has become slow.

We have searched and got the answer that Pagination might help to make app load fast.

We have also shared this problem and asked it @ stack-overflow .

As we are newbies, we could not ask the question in right way, then a member of stackoverflow helped us and contributed it look like a Question.

But unfortunately we havent recieved any response from Stackoverflow community.

We are trying here as well cause we need help.

Sharing with you all the post that We had asked @stackoverflow

I have developed an Android app by following INSTAGRAM App with Firebase - Android Studio Tutorial on YouTube.

The code works fine, but the issue is all the posts are loading same time making app slow. I want to show only 10 initial posts then want to load the next 10 and so on.

Could you help me achieve this?



private void readPosts() {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            postLists.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Post post = snapshot.getValue(Post.class);
                for (String id : followingList){
                    if (post.getPublisher().equals(id)){
                        postLists.add(post);
                    }
                }
            }
            postAdapter.notifyDataSetChanged();
            progressBar.setVisibility(View.GONE);
        }
    }
}



The above code works fine and posts are laoded but slow.




 

Hiranya Jayathilaka

unread,
Jul 16, 2020, 5:53:04 PM7/16/20
to fireba...@googlegroups.com
Look at how to use the Query API to order, filter and limit results: 

https://firebase.google.com/docs/database/android/lists-of-data#limit_the_number_of_results

Assuming you're using the auto-generated keys for your posts, you can easily query for the most recent 10 posts (see the example in the docs). Then you can grab the ID of the oldest post in that batch, and run another query using the startAt() function to fetch the next batch. Hope this helps.

Thanks,
Hiranya

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/cae46899-5c32-4d09-a53e-3476cc9d4373o%40googlegroups.com.


--

Hiranya Jayathilaka | Software Engineer | h...@google.com | 650-203-0128

Aijaz Sarfaraz

unread,
Jul 18, 2020, 11:19:51 AM7/18/20
to fireba...@googlegroups.com
Thank you for your response.

Thank you so much it helps a lot. :) :)

Reply all
Reply to author
Forward
0 new messages