r/mAndroidDev • u/That_Lonely_Soul_07 • Jun 11 '25
Lost Redditors π Flow + onStart + WhileSubscribed feels flawed β how do you handle initial data loading?
Guys, how to load initial data properly? I always use the init {} block in the ViewModel. But it's okay as long as you do not write tests. Then I read some articles and watched videos; they all used a common approach which was using the onStart operator and then stateIn with sharing strategy WhileSubscribed. I think this is a flawed approach because if the user navigates to the next screen and does not come back within the specified time (which is used in WhileSubscribed(time)) and comes back after the specified time has passed, then the flow will restart. So let's assume if you have some API calls in onStart, it'll get called again. Now suppose if we use the sharing strategy lazily, then the flow will never stop even after the last subscriber disappears.
So I want to know how you guys load initial data in the proper way? I know this is a shitposting sub, but most of you folks are experienced, and itβll help me understand this better.
20
u/hellosakamoto Jun 11 '25
This becomes a problem when people moved to kotlin ,coroutines , jetpack compose. Java XML asynctask don't have this problem.
12
u/PattaFeuFeu Jun 11 '25
Have you tried with Flutter?
10
u/PaipenTvantickZent @Deprecated Jun 11 '25
Just use flubber with AsyncTask.
But seriously, you're basically saying that you want a StateFlow that stays both active and not active when there are no subscribers. What you really want is the flow to stop when the screen is popped off the backstack, eg you can't navigate back to it. In that case the "Lazily" approach is all you need. The flow will stop when you use the viewModelScope as the scope for the stateIn parameter.
6
u/majster0s Deprecated is just a suggestion Jun 12 '25
You picked the wrong sub mate.
btw. Have you tried deprecating flow in favor of asynctask?
3
u/turelimLegacy Jun 11 '25
In latest project I went with Store5 but not expose it further than the repo layer: Flow<Data> + suspend get(fresh: Boolean): Data. Since it has a memory cache and you can configure it, store decides when to fetch the data screw the ui. Only the suspend method throws an error so you still need to call get(false) probably in onStart when collecting the flow to show it to the user if something fails on first load or when doing a pull to refresh by calling get(true). Store won't care if the ui will start/stop/start/stop collecting the flow or call get() from multiple places and will try to limit duplicate requests to the same api if you manange its keys correctly.
Hopefully that helps. If not, switch to FlubberAsyncTask.
2
u/theJakester42 Jun 12 '25
Ideally, your flow is exposed from a useCase or repo. You rely on that to do any sort of caching. It might be right to cache it to disk or memory. Or it might be right to make the API call for every new subscriber. Thats up to the repo. Idk who is using onStart like that, but don't.
0
u/[deleted] Jun 11 '25
[deleted] β view deleted comment