0

I am using firebase firestore as my backend. I have a recyclerView where I should load my data, I found about firestorePagingAdapter and it works fine for loading data from firestore when data is needed(when user scrolls down the list), problem is that I want to cache that data in my room database so if user switches from screen and comes back it won't fetch data from firestore but from room database.

Is it possible to do something like that, since my recyclerView extends FirestrePagingAdapter there is no way for me to call repository (I'm using mvvm) functions for writing data in db from recyclerView class. I read that paging library can be used with room database + network as data sources but I haven't found any working example and documentation is really confusing and I don;t think it can work with FirestorePagingAdapter.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alen
  • 949
  • 3
  • 17
  • 37

1 Answers1

0

The paging adapter provided by Firestore is not really "compatible" with the paging adapter provided by Jetpack. The one provided by Firestore is solving the problem for Firestore specifically, and the Jetpack component is providing a framework for generalized paging.

There is also no provided library that caches Firestore documents in Room. If you need to do that for whatever reason, you will have to find a way to do that on your own, or borrow someone else's. Firestore caches its own documents by default, so normally you don't have to worry about caching.

Stack Overflow isn't an appropriate forum to ask for examples, as that is considered off-topic.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • "Firestore caches its own documents by default", so if I understand correctly if I have some documents loaded with firestore paging adapter they are goind to be cached if I switch screen and than come back after few seconds, I have a bottom navigation with three fragments and in one of them I need to load data in recyclerView and I use firestorePagingAdapter, problem is if I switch to different fragment and come back later the adapter makes new fetch for same data which is unnecessary,I would like to cache that data and show it instead of doing another fetch every time the fragment is opened – Alen Jul 24 '19 at 08:30
  • Yes, all queries will use the same cache. – Doug Stevenson Jul 24 '19 at 17:00
  • So I tested it again to see when fetches are made and I can see that in onLoadingStateChanged ->LOADING_INITIAL is called every time when I switch back to that screen with recyclerView, and when I scroll LOADING_MORE and LOADED are called, does it mean that first time calls are made to firestore database and every next time cached documen data is used, – Alen Jul 25 '19 at 12:46
  • I'm not sure I understand your question. I since you're asking a different question than the one you started out with, please post a whole new question explaining what's not working the way you expect. – Doug Stevenson Jul 25 '19 at 15:48