Replies: 3 comments 1 reply
-
You shouldn't need to pass
Also, note that the coroutine scope returned by But, how is this situation different from any other "thing" that a presenter needs to do its job? Use proper dependency injection (e.g. Koin) and supply a
|
Beta Was this translation helpful? Give feedback.
-
We almost never do raw IO operations in presenters. They are handled with data layers that live at a longer lived scope than presenters. Cannot stress enough that Circuit wants you to use a good DI system + good data layer and just make presenters interpreters of that. If you start trying to do things like raw networking or otherwise-long-running tasks directly in your presenters and beholden to their lifecycles, you will have a bad time. |
Beta Was this translation helpful? Give feedback.
-
@valeriyo I agree. CoroutineScope should not be passed down to lower layers. This can cause leaks or unintended behaviour.
I mean below. Let's say we have many events which needs to invoke suspending functions from Domain/Data layer. To call these methods we need a scope (assume these are tasks are tied to the screen). Since the scope is local to This can be avoided if the scope is available as a class property. If the scope needs to outlive the screen, then it needs to be injected. But in cases where the job is tied to the screen, then it makes sense to create a scope. Chris Banes is experimenting around this in his tivi project: chrisbanes/tivi#1918 class ScreenAPresenter : Presenter<ScreenAUiState> {
@Composable
fun present(): ScreenAUiState {
val scope = rememberCoroutineScope()
return ScreenAUiState(
....
) { event ->
when (event) {
EventA -> doA(scope, ...)
EventB -> doB(scope, ...)
EventC -> doC(scope, ...)
...
}
}
}
private fun doA(scope: CoroutineScope, ...) {
scope.launch {
withContext(appDispatchers.io) {
....
}
}
}
private fun doB(scope: CoroutineScope, ...) {
scope.launch {
withContext(appDispatchers.io) {
....
}
}
}
private fun doC(scope: CoroutineScope, ...) {
scope.launch {
withContext(appDispatchers.io) {
....
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Generally in many cases Presenter would require a
CoroutineScope
to do background work. At the time of this writing, we can create it usingrememberCoroutineScope
insidepresent()
method. As this is local variable, we need to pass it down to functions which require them.Suggestion to introduce
CoroutinePresenter
which contains coroutineScope as a immutable property within. This would be similar to above approach but would be created by Circuit and can be available as a class level property.I would like to initiate a discussion and understand the feasibility of this idea
Beta Was this translation helpful? Give feedback.
All reactions