Hi, When I update from compose `1.6.0-alpha01` to ...
# compose
o
Hi, When I update from compose
1.6.0-alpha01
to
1.6.0-alpha02
my
Pager
is broken. The items are not loaded and the
PagingData
is not updated. I couldn't see any
androidx.paging
related change in the commits but maybe something else on the commit list causes this.
i
Upgrading your Compose version does not upgrade your
androidx.paging
version - those are totally decoupled from one another
o
ofcourse they are 🙂 but somehow my list is broken. I know it's very hard to tell what's wrong from the information I gave but I thought it would give you a hint. I didn't investigate further yet but what I know is when I update the compose,
PagingData
is not updated.
I found out that there was a mistake on the ViewModel. Pager was created like this:
Copy code
fun page(type: String) = Pager(
    config = PagingConfig(pageSize = 10),
    pagingSourceFactory = {
        TenantPagingSource(getTenantListUseCase, type)
    }
).flow.cachedIn(viewModelScope)
and collected like this:
viewModel.page("shop").collectAsLazyPagingItems()
This works with
1.6.0-alpha01
but doesn't work with
1.6.0-alpha02
When I fix it like this
Copy code
val page = Pager(
    config = PagingConfig(pageSize = 10),
    pagingSourceFactory = {
        TenantPagingSource(getTenantListUseCase, "shop")
    }
).flow.cachedIn(viewModelScope)
or collect like this:
Copy code
val shops = remember {
    viewModel.page("shop")
}.collectAsLazyPagingItems()
it works on the alpha02 too: But I really wonder how it's related to the compose version.
i
Sounds like your screen is recomposing - creating a new
Pager
every time your
page
function is called was only ever working by chance and was never how you should have had it set up
It sounds like what you should be doing instead is having a MutableStateFlow to represent your type and using
flatMapLatest
on that flow to create the Pager and return the Flow from it, then cachedIn the result. Then whenever your type changes, a new Pager and PagingSource gets created
I don't know if changing type is even something you expect to happen? Generally, that would be a fixed value that you'd already have in your ViewModel (i.e., it would be an argument to your screen that would be automatically available via
SavedStateHandle
, so you wouldn't need to pass the type manually to your ViewModel ever
o
Thank you for your recommendations @Ian Lake. There are only two fixed types, so I will just fix the bug by having two "pages" in my ViewModel. But it was really interesting to see that it was working before alpha02.