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

Marko Savic

10/18/2023, 1:26 PM
Hi there 👋 My name is Marko Savic and I am Sr. Android Engineer at New York Times 🙂 I have integrated
Paging3
library into our Article comments UI - with Jetpack Compose
Copy code
collectAsLazyPagingItems()
support and we also have implemented
jetpack compose navigation
( from comments page to comments thread page. ) Issue that now we have is that when animation navigation happens it triggers API call couple of times during animation and I don't know why that happens!!! 😢 Issue is similar to this one: https://stackoverflow.com/questions/71410790/paging-3-list-auto-refresh-on-navigation-back-in-jetpack-compose-navigation and I have tried saving the instance of Pager but that didn't help 😢 Of course I am using:
Copy code
flow.cachedIn(viewModelScope)
and Paging3Version is
3.2.0
Btw, I didn't have a luck to find any complex example like in my case where you have Paging3 + JetpackCompose + navigation Also we have Tabs there but it's another part of story. Does anyone has experience around this problem? thank you color 🙇
i

Ian Lake

10/18/2023, 1:59 PM
it triggers APl call couple of times during animation
This sounds like you are doing something as part of composition (as it is expected that your screen recomposes multiple times during an animation), but you shouldn't ever be triggering anything API call related as part of composition or as part of an Effect really - Paging automatically refreshed when your PagingSource invalidates itself
We discussed this on Twitter a while back: https://twitter.com/ianhlake/status/1692754799387640043
m

Marko Savic

10/18/2023, 2:05 PM
I appreciate @Ian Lake I will take a look
Hey @Ian Lake 👋 I appreciate your help on this one but I still didn't find working solution for my problem. I tried 3 or 4 different approach but result is still the same - during animation I am getting multiple API call's for the same route (and params). Because of that I decided to write an diagram so it would be easier for your to understand the situation. Result of this code is this LOG:
Copy code
<--- ANIMTION start --->
enterTransition Blue
exitTransition Red
enterTransition Blue
exitTransition Red
<--- ANIMTION end --->

<--- UI load --->
Render Blue

<--- ANIMTION start --->
enterTransition Blue
exitTransition Red
<--- ANIMTION end --->

<--- UI load --->
Render Red

<--- API CALL --->
load load load

<--- UI load --->
Render Blue

<--- API CALL --->
load load load
Did ViewModel clears cache somehow? Does load method calls for not reason? Thanks! 🙏 🙇
i

Ian Lake

10/23/2023, 9:39 PM
Yeah, your ViewModel is wrong: you're creating a new Pager and a new Flow every composition. Store that Flow as a variable in your ViewModel
I.e., it should be a
val
, not a
fun get
m

Marko Savic

10/23/2023, 9:41 PM
oh that's the problem, thanks! let me try
thank you color
Omg it's woring @Ian Lake such a small thing 😞
🎉 1
You saved the day 🙏
i

Ian Lake

10/23/2023, 10:18 PM
We actually specifically updated the docs to call this out just a week and a half ago (which will update on the next Paging release): https://android-review.googlesource.com/c/platform/frameworks/support/+/2788440
1
m

Marko Savic

10/24/2023, 12:06 AM
Great, thanks!