https://kotlinlang.org logo
#compose
Title
# compose
f

FunkyMuse

06/23/2021, 3:47 PM
Does
Copy code
collectAsLazyPagingItems()
respect something like
Copy code
flow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
or the data is updated even when the UI isn't in started mode?
a

Alexandre Elias [G]

06/24/2021, 12:14 AM
yes, I believe that should be respected
for future reference, the usual recommended way to pause work in Compose between stop and start is to put the workload inside
withFrameNanos
in an Effect, but you can't do that here since the Effect lives in private code you can't modify. so using
flowWithLifecycle
seems right
f

FunkyMuse

06/24/2021, 9:24 AM
Thanks Alexandre, my question was since the Provided function doesn't respect that, are we gonna get something in the future that does this or we have to build it for ourselves?
a

Alexandre Elias [G]

06/24/2021, 5:45 PM
my understanding of how it works right now is:
pagingDataFlow.collectAsLazyPagingItems()
waits for create lifecycle event, but is unaffected by stop and start events. whereas
pagingDataFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED).collectAsLazyPagingItems()
is additionally paused between stop and start events.
I can imagine different developers preferring one or the other and the current way has flexibility to choose, so I'm not sure yet whether Compose-side API will provide anything additional in the future. it's an interesting question we might discuss further though, especially if we hear the question repeatedly
please file a bug if you have observed that in fact, ``pagingDataFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED).collectAsLazyPagingItems()`` does not work as desired for some reason. then we are more likely to make a change in Compose
f

FunkyMuse

06/24/2021, 10:49 PM
Thanks Alexandre will try that tomorrow and file a bug if it doesn't work, i was just curious how this were for the future and whether i need to build this myself
Copy code
pagingData.flowWithLifecycle(LocalLifecycleOwner.current.lifecycle).collectAsLazyPagingItems()
it doesn't seem to display the data 😕
a

Alexandre Elias [G]

06/29/2021, 5:09 AM
a

Adam Powell

06/29/2021, 11:05 PM
If that call recomposes it'll start anew since
flowWithLifecycle
will return a new flow instance, I think what you may want is this:
Copy code
val lifecycle = LocalLifecycleOwner.current.lifecycle
val lazyPagingItems = remember(pagingData, lifecycle) {
  pagingData.flowWithLifecycle(lifecycle)
}.collectAsLazyPagingItems()
the
remember
will make sure it doesn't reassemble a new flow chain on recomposition unless you truly have a different
pagingData
source or
lifecycle
for that recomposition
67 Views