https://kotlinlang.org logo
#compiler
Title
# compiler
u

ursus

03/13/2023, 5:17 PM
Copy code
class FooViewModel(
    private val applicationScope: CoroutineScope
    private val someUseCase: UseCase
) {
    fun fooClicked() {
        applicationScope.launch {
            someUseCase.doWhatever() <-----------
        }
    }
}
the
launch
lambda captures this, to access the
someUseCase
field. Is there a way to make the capture explicit somehow, like I could in swift? (There is a temporary viewmodel this leak until the coroutine completes) Or does this have to be redesigned (i.e. some class that owns the scope and runs the use case on it, so no FooViewModel.this is captured?
n

natario1

03/13/2023, 8:16 PM
val someUseCase = someUseCase
before launch should do the job
u

ursus

03/14/2023, 12:03 AM
neat, thanks!
7 Views