Guilherme Delgado
08/12/2021, 8:42 AMintent
, I’ll add details in thread:override val container = container<LandingState, LandingSideEffect> (LandingState(hasSession = sessionManager.hasSession()), stateHandle) {
intent {
//runBlocking
}
}
If I’m querying async data (network or repo) what’s the right approach?
A:
intent {
viewModelScope.launch {
if (!sessionManager.hasSession()) {
//val result = ...fetch online operation
state.copy(hasSession = result)
}
}
}
B:
viewModelScope.launch {
if (!sessionManager.hasSession()) {
//val result = ...fetch online operation
intent {
state.copy(hasSession = result)
}
}
}
My confusion is regarding the runBlocking
from intent
:
@OrbitDsl
public fun <STATE : Any, SIDE_EFFECT : Any> ContainerHost<STATE, SIDE_EFFECT>.intent(
registerIdling: Boolean = true,
transformer: suspend SimpleSyntax<STATE, SIDE_EFFECT>.() -> Unit
): Unit =
runBlocking {
container.orbit {
withIdling(registerIdling) {
SimpleSyntax(this).transformer()
}
}
}
Mikolaj Leszczynski
08/12/2021, 8:57 AMintent
will launch a new coroutine internally.
You can see real examples in our samples and worth reading through our documentation, but generally something like this will be enough:
intent {
if (!sessionManager.hasSession())
val result = networkCall() // Suspending function running within IO context
reduce {
state.copy(hasSession = result)
}
}
}
Guilherme Delgado
08/12/2021, 8:59 AMThreading guarantees#
• Calls to Container.intent
do not block the caller. The operations within are offloaded to an event-loop style background coroutine.`
• Generally it is good practice to make sure long-running operations are done in a switched coroutine context in order not to block the Orbit "event loop".
found it!Mikolaj Leszczynski
08/12/2021, 9:02 AMintent
miqbaldc
08/12/2021, 12:09 PMviewModelScope.launch
anymore even we use AndroidX ViewModel
? cmiiwMikolaj Leszczynski
08/12/2021, 12:19 PMviewModelScope.launch
ever something that we recommended? I think it should be avoided because you’re launching a coroutine outside of orbit container’s scope. If you’re thinking about coroutineScope { }
then that is only needed for parallel decomposition or if you absolutely need to launch a coroutine within intent
Guilherme Delgado
08/12/2021, 3:37 PMintent
, but the container is scoped to ViewModel lifecycle or am I wrong? 🤔
creating containers scoped with ViewModelScope to automatically cancel the Container whenever theis cleared.ViewModel
Mikolaj Leszczynski
08/12/2021, 3:58 PMappmattus
08/13/2021, 1:00 PM