Can anyone help me? ```// Data Layer val location...
# coroutines
i
Can anyone help me?
Copy code
// 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)
Copy code
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)
1
u
@Irsath Kareem This is hard to parse. Is the problem that you are trying to address that on initial composition you are seeing
null
? 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.
Also, getting a location fix from the OS can take a while in general, depending on how you do it.
@Irsath Kareem I see that you seem have resolved your issue on another channel shortly after you posted your issue here. Mind updating your other outdated issues at that point? I doubt I will volunteer to help you out again.
r
SharedFlow, is completely Hot , meaning its easy to miss emissions if you don't collect from them on time. the reason why it works in your UI, is because you are using Stateflow, which means anytime you collect from it ,it emits the latest value. Thats all there is to it
if you set the replay cache on the sharedFlow to 1, it would work
or just use a stateflow, or a channel