0

I wanted to build an application like Twitter where I have to display a list of data sets with live updates for counting like and comments in real time.

For that, I am using

  1. RecyclerView - for displaying list of scrollable data
  2. Jetpack LiveData - for observing changes and notifying observers.
  3. Firestore - As a backend for getting realtime data with snapshot changes.
  4. Paging 3 - for loading large set of data into list by chuncks.

But how would I implement the firestore query with PaggingSource class by emitting live changes to the list?

class FirebasePagingSource() : PagingSource<QuerySnapshot,ItemModel>() {

override fun getRefreshKey(state: PagingState<QuerySnapshot, ItemModel>): QuerySnapshot? {
 // Need guidance for implementation
}

override suspend fun load(params: LoadParams<QuerySnapshot>): LoadResult<QuerySnapshot, ItemModel> {
 //Need guidance for implementation
} }

Here ItemModel is a data class

2. ItemListLiveData Class

class FirestoreQueryLiveData(private val query: Query)
: LiveData<List<Item>>(), EventListener<QuerySnapshot> {

private lateinit var registration: ListenerRegistration

override fun onActive() {
    super.onActive()
    registration = query.addSnapshotListener(this)
}

override fun onInactive() {
    super.onInactive()
    registration.remove()
}

override fun onEvent(snapshot: QuerySnapshot?, error: FirebaseFirestoreException?) {
    snapshot?.let {
        val documents= mutableListOf<Item>()
        for (document in snapshot.documents) {
            document.toObject(Item::class.java)?.let {
                documents.add(it)
            }
        }
        postValue(documents)
    }
} }
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo May 21 '21 at 10:59
  • Maybe this article, [How to paginate Firestore using Paging 3 on Android?](https://medium.com/firebase-tips-tricks/how-to-paginate-firestore-using-paging-3-on-android-c485acb0a2df) together with this [repo](https://github.com/alexmamo/FirestorePagination) will help. – Alex Mamo May 21 '21 at 11:00
  • And if you want to use Jetpack Compose, here is another [article](https://medium.com/firebase-tips-tricks/how-to-implement-pagination-in-firestore-using-jetpack-compose-76b4c0b5acd5). – Alex Mamo Jan 28 '22 at 09:24

1 Answers1

2

There are plenty of useful guides online. In general, you can set a stream of paged data from Firestore into a Recycler View and from there it is simply a matter of paginating each request.

This can be a lengthy and involved process, so I will link a recommended tutorial, its source, and the relevant youtube guide.

DIGI Byte
  • 4,225
  • 1
  • 12
  • 20
  • .await () is not working-> unresoulable reference –  May 23 '21 at 13:12
  • `.await()` is not a thing - you should evaluate your code and ensure it is equivalent with any tutorial you are following – DIGI Byte May 26 '21 at 23:54
  • For .await() to work, you must first enable Courotine for firebase. Just add these 3 dependencies implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.6.0" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1" implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1" – Martin Mbae Apr 30 '22 at 08:49