1

The first activity is a News Feed screen, where RecyclerView + Firestore is used. I have the Settings Offlline option enabled.

Problem:

Because it is an application home page and a newscast, I would like the application to display the cached data while internally scanning and retrieving the Firestore data. Like Instagram, Twitter, etc ...

Because currently when opening this activity, it is already called Firestore Query and mounted the data with RecyclerView.

It would have some way of showing the cached data of the last update of Recyclerview while the new data of the Firestore is loaded.

Because when the internet connection is mobile, the quality is very poor in our country. And if I leave calling Recyclerview initially, I'd have a loading Progressbar for a long time, until the connection stabilizes.

My code is: Adapter Firestore + RecyclerView

EDIT CODE:

 /*Firebase*/
        mDb = FirebaseFirestore.getInstance();
        mDb.collection("IndexTimeline_User").document(mCurrentUser.getUid()).collection("Posts").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshots) {

                mQuery = documentSnapshots.getQuery();
                mCardFeedList.setAdapter(mAdapter);


            }
        });
        mDb.setFirestoreSettings(settings);


        /* Recycler */
        mCardFeedList = (RecyclerView) view.findViewById(R.id.explore_tab1_list);
        mCardFeedList.setHasFixedSize(true);
        mCardFeedList.setItemViewCacheSize(20);
        mCardFeedList.setDrawingCacheEnabled(true);

        mAdapter = new PostsExploreTab1Adapter(mQuery, this){

            @Override
            protected void onDataChanged() {

                if (getItemCount() == 0) {
                    mCardFeedList.setVisibility(View.GONE);
                    mProgressBar.setVisibility(View.GONE);
                    //mTxtVazio.setVisibility(View.VISIBLE);

                } else {
                    mCardFeedList.setVisibility(View.VISIBLE);
                    mProgressBar.setVisibility(View.GONE);
                    //mTxtVazio.setVisibility(View.GONE);
                }


            }
        };

        llmanager = new LinearLayoutManager(getActivity());
        mCardFeedList.setLayoutManager(llmanager);




        /*INICIADO O RECYCLERVIEW E ADAPTER*/
        if (mAdapter != null) {
            mAdapter.startListening();

        }

Thanks.

TiagoIB
  • 439
  • 7
  • 25
  • For future visitors, you can take a look **[here](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)**, where I have explained step by step how to display data from Firestore into a `RecyclerView` using Android. – Alex Mamo Mar 14 '18 at 13:08

1 Answers1

1

When you are getting data from the database, use an addOnSuccessListener and set the adapter only when the data is loaded from the database. This means that you'll read the data from the cache and only when the new data is fully loaded and only then will be added to the RecyclerView. Your code should look like this:

yourRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        //get data
        mCardFeedList.setAdapter(mAdapter);
    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • but would I do this query here? and then set up the query? (mQuery = mDb.collection("IndexTimeline_User").document(mCurrentUser.getUid()).collection("Posts");) @AlexMamo – TiagoIB Dec 21 '17 at 13:22
  • If `yourRef` is equal to that, yes, use `get()` method and then `addOnSuccessListener`. – Alex Mamo Dec 21 '17 at 13:24
  • my reference is exactly as it is in the question.Will it work? http://prntscr.com/hqj4zt @AlexMamo – TiagoIB Dec 21 '17 at 13:30
  • Very important, you need to set the adapter only once, in `addOnSuccessListener`. You need to remove the last line where you set the adapter again. So yes, should work. Have you tryed? – Alex Mamo Dec 21 '17 at 13:33
  • I can not now. In a few moments, I'll test. Excuse me. @AlexMamo – TiagoIB Dec 21 '17 at 13:37
  • Ok, keep me posted. – Alex Mamo Dec 21 '17 at 13:38
  • did not work. My mQuery expects Query and when I change to AddOnSucess, it transforms into QuerySnapshot @AlexMAmo – TiagoIB Dec 21 '17 at 14:41
  • And you cannot get the data from `QuerySnapshot`? Or what is the problem now? What is the error? – Alex Mamo Dec 21 '17 at 14:56
  • I believe that the problem is elsewhere. I have to capture only the Id of the posts, and inside the Adapter go on another node and pick up the data from that node, such as photo, name, date and time. So I'm going to continue this slow search. From what i understand. @AlexMamo – TiagoIB Dec 21 '17 at 15:02
  • If you get other data that you expect it means that you point to a wrong reference. Change the reference to contain the desired values. – Alex Mamo Dec 21 '17 at 15:08
  • but thanks for the help because in a way it worked yes as you explained. @AlexMamo – TiagoIB Dec 21 '17 at 15:08