Irsath Kareem
09/08/2025, 10:20 AM// Data Layer
val locationUpdatesFlow = callbackFlow {
// Registered Location Callback
awaitClose {
// Cleanup
}
}.shareIn(
scope = applicationScope,
sharingStarted = SharingStarted.WhileSubscribed(),
)
The reason, Why I am using SharedFlow in Data layer is, This flow can be collected in background also (by some other services....) while we collecting via UI. So, it will be performant that, the locationUpdatesFlow is SHARED. (I think so).
Also, I need, this flow is running, if at least one subscriber is available. Otherwise, Kill this Flow. For this reason, I use SharingStarted.WhileSubscribed()
Now In UI Layer (ViewModel)
val locationsState = DataLayer.locationUpdatesFlow
.stateIn(
scope = viewModelScope,
sharingStarted = SharingStarted.WhileSubscribed(5000),
initialValue = null
)
The reason, Why I am using STATEFLOW here is, I need the location value in my UI as observable value
In Screen, I use viewModel.locationsState.collectAsStateWithLifecycle()
The Problem, I faced is below,
1. DataLayer.locationUpdatesFlow is NOT started....... While my UI is UP and RUNNING
Is there any problem, that two nested SharableFlow (One on another) - (StateFlow on SharedFlow, in this case)
Can any one explain this, And how to resolve this? (Also considering my Data Layer usecases/needs)Uli Bubenheimer
09/08/2025, 2:34 PMnull? That would be more of a compose question, and there is no single best way to address it.
Making a StateFlow from a SharedFlow should principally work (yet it does not address your initial null problem), but what you are doing is really messy, in particular as you are then additionally keeping Compose State on top of 2 levels of flow state, that's 3 separate state holders for essentially the exact same thing.Uli Bubenheimer
09/08/2025, 2:48 PMUli Bubenheimer
09/08/2025, 7:58 PMrkeazor
09/10/2025, 1:31 PMrkeazor
09/10/2025, 1:32 PMrkeazor
09/10/2025, 1:33 PM