Travis Reitter
04/18/2023, 4:38 PMCoroutineContext
in the public constructor and internally creating a new scope from it. Is that a reasonable way to handle the CoroutineScope
for the controller and how do I need to handle the lifecycle of the scope? Details in the threadTravis Reitter
04/18/2023, 4:40 PMclass RecipeDetailBloc internal constructor(
val dependencies: Dependencies, // also contains a com.arkivanov.essenty.lifecycle.Lifecycle
private val mainContext: CoroutineContext,
private val mainScope: CoroutineScope,
) {
constructor(dependencies: Dependencies) : this(
dependencies = dependencies,
mainContext = mainDispatcher,
mainScope = CoroutineScope(mainDispatcher)
)
private val recipeDetailStore =
dependencies.instanceKeeper.getStore {
RecipeDetailStoreFactory(
storeFactory = dependencies.storeFactory,
ingredientService = dependencies.ingredientService,
ingredientRepository = dependencies.ingredientRepository,
recipeDetailUseCase = dependencies.recipeDetailUseCase,
mainContext = mainContext,
).create()
}
// presumably, I need to add this (haven't tested yet):
fun destroy() {
mainScope.cancel()
}
...
is handling it via destroy()
all that I need to do? And is it the best way to handle it in the context of controller being used in an MVIKotlin setup?Arkadii Ivanov
04/18/2023, 4:43 PMTravis Reitter
04/18/2023, 4:46 PMCoroutineScope(mainDispatcher)
still makes sense, right? Seems safer than having both the context and scope as constructor arguments in case the caller provides two unrelated onesArkadii Ivanov
04/18/2023, 4:47 PMmainContext: CoroutineContext
and create CoroutineScope internally. This allows swapping the context with a test dispatcher in tests, yet the scope is encapsulated.Arkadii Ivanov
04/18/2023, 4:48 PMTravis Reitter
04/18/2023, 4:55 PMmainDispatcher
which is Dispatchers.Main.immediate
. I guess I should pass it in via dependencies
for flexibility though I don't think I'd pick a different oneArkadii Ivanov
04/18/2023, 4:59 PMTravis Reitter
04/18/2023, 5:01 PMTravis Reitter
04/18/2023, 5:02 PMArkadii Ivanov
04/18/2023, 5:04 PMTravis Reitter
04/18/2023, 5:08 PMTravis Reitter
04/18/2023, 5:08 PMArkadii Ivanov
04/18/2023, 5:09 PMTravis Reitter
04/18/2023, 5:13 PM