Patterns: Combining RecyclerView, LiveData and Firebase Cloud Firestore

from the CommonsWare Community archives

At July 11, 2019, 3:28pm, jQrgen asked:

Hello CommonsWare & Forum!

Has anyone got pointers on the best patterns for combining RecyclerView and LiveData using firebase firestore?

Also, simple patterns for combining RecyclerView and LiveData are appreciated!

Thanks.


At July 11, 2019, 4:00pm, mmurphy replied:

I have never used Firestore, sorry.

That is very broad. FWIW, I show some in Elements of Android Jetpack, such as in the Room chapter. Also, the to-do app built in the tutorials in Exploring Android use LiveData with RecyclerView in the RosterListFragment.

In these, I use a variant of the Model-View-Intent (MVI) architecture. The LiveData is emitting view-states, containing the data to be displayed. The activity or fragment observes the LiveData and renders the UI based on that view-state. In the case of RecyclerView, the view-state contains a List of row-states to be rendered, and the RecyclerView.Adapter and RecyclerView.ViewHolder implementations handle that. My samples mostly use ListAdapter as the RecyclerView.Adapter base, so the LiveData observer just calls submitList() with the list of row-states, and the rest is handled by the RecyclerView stuff.