Hi. I have a long running flow (compose 1.2.1), w...
# coroutines
j
Hi. I have a long running flow (compose 1.2.1), which has several flatMapLatest calls on other observables. When the user logs out an observable near the top of the flow emits. I am having trouble seeing how to interrupt the downstream observables, which are seeing the missing data after logout and attempting to request updates. One of the ideas I tried was
Copy code
observeLoginState().map { it is LoggedIn }
.distinctUntilChanged()
.flatMapLatest { isLoggedIn ->
    if (isLoggedIn) { dataSource.observe() }
    else { emptyFlow() }
}
...
.flatMap {}
,,,
,launchedIn(scope)
However it really seems like the downstream flatMaps are continuing to observe even when a flatMapLatest upstream has changed. Am I doing something wrong? Is there a way to cancel the downstream observers while keeping the flow alive? A couple of other ideas I tried: • Threading a null or sentinel value through the flow so downstream observers could be swapped out but requires too many compromises about the type of data based along the flow. • throwing an exception and using retryWhen to restart the flow. I ran into an exception retry loop when logged out which I should have seen even before I tried. Even though I expect it could be made workable, I didn’t really spend the time looking further as it felt kludgey.
b
For those who find this in the future through search - the solution was to nest dependent flatMaps instead of chaining them. Nesting allows downstream to be cancelled properly.