1

I have the following code, but I don't think the pagination is implemented.

dao

interface IArticleDao {
    @Query(
        """
        SELECT * FROM t_article ORDER BY :order DESC
    """
    )
    fun pagingSource(order: String): PagingSource<Int, Article>
}

Repository

class ArticleRepository @Inject constructor(
    private val articleDao: IArticleDao
) {
    fun list(order: String) = articleDao.pagingSource(order)
}

ViewModel

@HiltViewModel
class ArticleViewModel @Inject constructor(private val articleRepository: ArticleRepository) : ViewModel() {
    fun list(order: String) = Pager(PagingConfig(pageSize = 20)){
        articleRepository.list(order)
    }.flow.cachedIn(viewModelScope)
}

Screen

    val articleViewModel = hiltViewModel<ArticleViewModel>()
    val lazyArticleItem = articleViewModel.list("id").collectAsLazyPagingItems()

    ArticlePage(lazyArticleItem)

ArticlePage

    LazyColumn{
    items(...)
    when(val state = lazyArticleItem.loadState.append){
                is LoadState.Error -> {
                    println("error")
                }
                is LoadState.Loading -> {
                    println("${lazyArticleItem.itemCount}, ${lazyArticleItem.itemSnapshotList}")
                }
                else -> {}
            }
}

lazyArticleItem.itemCount printed the number 668, so I don't think the pagination is working properly, but the data displayed on the UI interface is fine, it's just not paginated by 20 items per page.

SageJustus
  • 631
  • 3
  • 9
  • @Klitos G. Sorry to bother you, I found this [question](https://stackoverflow.com/questions/74968933/i-dont-know-why-android-paging3-with-roomdatabase-and-compose-return-entire-dat) similar to mine and you answered it. But this problem has not been solved in the end, can you please take a look at my problem. – SageJustus Feb 03 '23 at 11:10

1 Answers1

2

In the PagingConfig, you can specify enablePlaceholders, which is true by default (your case). If placeholders are enabled and paging source knows the total number of items, which it knows when it takes data from room database, lazyArticleItem.itemSnapshotList size will be the total size of the source, only the elements that are not yet loaded will be null.

So you can't say that paging is not working based on itemCount. You are also printing itemSnapshotList, are there nulls? You can also try setting enablePlaceholders = false, itemCount then corresponds to the number of loaded items.

Jan Bína
  • 3,447
  • 14
  • 16
  • Yes, set enablePlaceholders is true can solve this problem. But if the default setting of Room is `enablePlaceholders=false`, then this is recommended. If there are tens of thousands of records in my table, won't querying the database once affect performance and memory? – SageJustus Feb 05 '23 at 07:36
  • No, it doesn't query the database for all the items. It only queries it for total count, which is cheap, and then it loads pages of items. So it knows the total count, but never actually access the items until you need them. – Jan Bína Feb 05 '23 at 10:57
  • I have another question I want to ask, can you see why the data I queried is not in reverse order according to id? The sql statement is `SELECT * FROM t_article ORDER BY id DESC`, The result set obtained by directly using the run sql query is in reverse order according to the id. – SageJustus Feb 05 '23 at 16:28
  • That's weird, I have no idea why this might be happening. If you are sure that two functions with the same sql statement, one returning `List` and the other `PagingSource`, produce result with different ordering, you should probably file a bug report to the room/paging library. – Jan Bína Feb 05 '23 at 23:28
  • Just tested, if the return type is `List`, then it is the correct result, and the order is reversed according to the id. `dao` returns `PageSource` instead of a custom `PageSource`, is it possible that this is the reason? – SageJustus Feb 06 '23 at 04:10
  • what does "dao returns PageSource instead of a custom PageSource" mean? – Jan Bína Feb 06 '23 at 08:41
  • I saw that the document has customized an ExamplePageSource inherited from PageSource `class ExamplePagingSource( ... ) : PagingSource()`. I see `PagingSource` that is directly returned in the Room documentation, not `ExamplePagingSource` – SageJustus Feb 06 '23 at 09:07
  • No, I don"t think so, it looks like a bug – Jan Bína Feb 06 '23 at 09:34
  • 1
    I found the problem, Room does not support passing `columnName` – SageJustus Feb 08 '23 at 05:43