Hello, I'm trying out Molecule like in <https://gi...
# squarelibraries
s
Hello, I'm trying out Molecule like in https://github.com/chrisbanes/tivi/pull/862/files?diff=split&amp;w=0 (ViewModel exposing the state flow produced by Molecule) and this state flow is consumed via
collectAsStateWithLifecycle
in UI composable. Now, when i background the app, will the coroutine launched by molecule still be active?
s
This doesn't have something to do with Molecule itself, your flow will ve paused if you give it information on when it will be paused using stateIn(SharingStarted.WhileSubscribed(seconds here)). So it all depends on how you create your StateFlow Otherwise it will keep being alive
yes black 1
s
@Stylianos Gakis stateIn works only when dealing with upstream cold flows. The flow from
launchMolecule
is a hot one. Updated using
moleculeFlow
and that did it. Thanks.
s
Yeap, hence I said "it all depends on how you create your StateFlow". Creating it with stateIn is the only way, if you've created another StateFlow with launchMolecule then that would not be what I suggested 😅
s
Got it, besides, is there any reason to use
launchMolecule
in Android, given lifecycle scoped observing logic in UI?
s
I haven't found a use of it myself, but we use AACVM and put our molecule presenters scoped to that so we always have to be careful with that. Perhaps in some other type of architecture it would make sense? Not sure
s
Okay, the other case i can think of is when the upstream flows aren't frequently updated or expensive and effectively becomes a no-op when app is backgrounded.
s
Yes, however I wouldn't rely on this myself. It's possible for a previously "cheap" flow to get another source of data which is quite expensive, and have this change happen in the data layer without any API changes and whatnot. In those scenarios you wouldn't want to have to go and check the call sites one by one to make sure nobody assumes that this was a cheap flow and needs changing. It's best to just do the right thing always and then you are free to make such changes without having to change unrelated code at the same time.