```val pendingDeeplink = MutableStateFlow<Deeplink...
# coroutines
u
Copy code
val pendingDeeplink = MutableStateFlowDeeplink?()
...
scope.launch {
    pendingDeeplink
        .filterNotNull()
        .collect { deeplink ->
            applyDeeplink(deeplink)
            pendingDeeplink.value = null
        }
}
is this a good or bad pattern? to null out a stateflow from the inside collector of said flow? or maybe in general a collector of stateflow changing it?
y
I feel like you want a channel instead (with a buffer of 1 maybe and drop oldest)
u
but channel says it's ready to do business before my deeplink is actually applied, which won't fit here 😕 unless im wrong
s
MutableSharedFlow?
u
same
anyways, i just wanna ask if its cool to mutate upstream from collector basically collects should be serialized so its .. fine?
s
What do you mean by 'ready to do business'?
u
that the value will be removed from the channel cache upon delivery, not upon having been processed,
s
A deep-link is sent through a notification, push, etc, which updates (emit/tryEmit) the
pendingDeepLink
then the
collect
body will receive it and act. Why do you need the value/event to remain until it has finished being processed?
y
You might want a second channel (or even a state flow) for when your ready
u
because the intent application might get cancelled midway due to orientation change etc
s
Ah, then it is a state, not an event....
If that is the case, why set it to 'null' after it is being processed?
u
so if processed, it doesnt replay on orientation change
s
But then it should be an event, not a state
And for events MutableSharedFlows should be used.
u
thats not orientation change safe, as you agreed
s
If navigation is modeled as an event, then use MutableSharedFlow. If you have modeled navigation (screens) as state, then use MutableStateFlow.
u
why are you suggesting shared flow, it only cares about delivery,i.e. collector was started, nothing more, if your code inside collector is suspending, then it might not finish completely and youll pretend its fine
Copy code
workDone = true
doWork()
s
Depends on when
collect
is called. Is the scope cancelled upon orientation change and the
collect
ends and then scope.launch is executed again, or does the
collect
remain active upon an orientation change?
if your code inside collector is suspending, then it might not finish completely and youll pretend its fine
yeah, that may be an issue.
u
Copy code
sharedFlow
  .collect {
      delay(10.seconds)
      work()
  }
at second 5, you do orientation change, work will never execute
s
Use withContext(NonCancellable) ? Then the work() could be done, but still the actual navigation and/or UI change should not happen.
u
that api is designed for native code that needs to clean up resources, destructors etc, not normal business logic
s
I think the 'work()' should be outside the lifecycle/scope of the
scope