ursus
08/11/2024, 10:02 AMclass MyVieWModel(
private val repository: Repository,
private val applicationCoroutineScope: CoroutineScope
) {
fun foo() {
applicationCoroutineScope.launch {
delay(10_000)
repository.doSomething()
}
}
}
is this pattern (using a injected coroutine scope that has longer life-time than a viewmodel does) and launching a coroutine in it considered bad?
I think it is, since the launch lambda captures the repository field, however there is from what I know there is no field capture in kotlin/java, and it always captures viewmodel this
thus leaking it (if screen is closed before the lambda returns)
(if it captures just the repo, then it would be fine -- but there's no way to do that in kotlin?)
am I correct?ef
08/11/2024, 10:05 AMursus
08/11/2024, 10:27 AMCLOVIS
08/11/2024, 12:53 PMrepository
will be captured, but I don't have the knowledge to be sure that's the case here. https://kotlinlang.org/spec/expressions.html#lambda-literalsephemient
08/11/2024, 9:36 PMephemient
08/11/2024, 9:38 PMsuspend
lambda to capture this@MyViewModel
at creation, then clear the reference after repository
is usedursus
08/11/2024, 10:11 PM