How to observe changed items from Room with Paging 3 library in Android
Asked Answered
S

0

8

I have a list of data which is loading by using PagingSource.

Here is my code in DAO:

  @Query("SELECT * FROM Transport")
  fun getAllTransportsPaged(): PagingSource<Int, Transport>

I am getting the paged data with a Flow with this:

Pager(config = PagingConfig(
          pageSize = 100,
          enablePlaceholders = true,
      )) {
        transportsDao.getAllTransportsPaged()
      }.flow
       .cachedIn(viewModelScope + Dispatchers.IO)

I have another function which is updating an item in a local database:

@Query("UPDATE Transport SET favorite = :favorite WHERE id = :id")
suspend fun changeFavorite(id: Int, favorite: Boolean)

After calling this function the whole data in the list is starting to load from the beginning.

Is there a way to get only changed data with paging library?

Smallman answered 27/4, 2021 at 12:32 Comment(6)
Do you mean that after you insert an item, RV reloads from scratch? That sounds like a bug in the library, could you share your project with me?Breastpin
Yes that is how it works. This is the code: @Insert suspend fun addTransport(vararg transport: Transport)Smallman
How are you consuming the Flow<PagingData> for your adapter? Room's PagingSource already has built-in support to resume where it left off after invalidation, so there must be some drastic changes happening in your DB or you might be on an old version of Paging? Can you also share your DiffUtil implementation? That could be another possibility for Paging to not understand rows that are supposed to be equalBreastpin
class TransportsDiffCallback : DiffUtil.ItemCallback<Transport>() { override fun areItemsTheSame(oldItem: Transport, newItem: Transport): Boolean = oldItem.id == newItem.id override fun areContentsTheSame(oldItem: Transport, newItem: Transport): Boolean = oldItem == newItem }Smallman
This is my implementation of DIffUtil.Smallman
Were you able to resolve this?Pretypify

© 2022 - 2024 — McMap. All rights reserved.