6

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?

  • 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? – dlam May 15 '21 at 05:08
  • Yes that is how it works. This is the code: `@Insert suspend fun addTransport(vararg transport: Transport)` – Robert Levonyan May 15 '21 at 06:34
  • How are you consuming the `Flow` 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 equal – dlam Jun 21 '21 at 01:01
  • `class TransportsDiffCallback : DiffUtil.ItemCallback() { override fun areItemsTheSame(oldItem: Transport, newItem: Transport): Boolean = oldItem.id == newItem.id override fun areContentsTheSame(oldItem: Transport, newItem: Transport): Boolean = oldItem == newItem }` – Robert Levonyan Jun 22 '21 at 15:15
  • This is my implementation of DIffUtil. – Robert Levonyan Jun 22 '21 at 15:16
  • Were you able to resolve this? – Naresh NK Aug 11 '21 at 11:52

0 Answers0