Is it okay to have long running intents like this:...
# orbit-mvi
b
Is it okay to have long running intents like this:
Copy code
fun <S : Any> ContainerHost<S, *>.observeFlow(
    flow: Flow<String>,
    block: S.(String) -> S
) {
    intent {
        flow.collectLatest { data ->
            reduce {
                state.block(data)
            }
        }
    }
}
Or is it better to do it in a CoroutineScope like this:
Copy code
fun <S : Any> ContainerHost<S, *>.observeFlow(
    flow: Flow<String>,
    scope: CoroutineScope,
    block: S.(String) -> S
) {
    scope.launch {
        flow.collectLatest { data ->
            intent {
                reduce {
                    state.block(data)
                }
            }
        }
    }
}
a
I’ve used the former and not seeing any apparent issues in our app with millions of users. But would love to hear from the library authors
m
1st option - every intent spawns a separate coroutine on the Default dispatcher, so it's safe to do. At my job (live e-commerce app), we're collecting many flows like that without issue, sometimes dozens on one screen. I recommend wrapping this in a
repeatOnSubscription
if the state doesn't need to be updated when there's no UI though (e.g. app backgrounded). I certainly do not recommend the latter, passing scopes in from the outside is generally something I personally consider a code smell, as it kinda circumvents the structured concurrency paradigm.