Is there any alternatives ways to provides a `view...
# dagger
m
Is there any alternatives ways to provides a
viewModelScope
? Moved to this issue instead with more details and examples: https://github.com/google/dagger/issues/2004
g
viewModelScope is not a dispatcher, it’s CoroutineScope You can get scope from it, using
Copy code
viewModelScope.coroutineContext[CoroutineDispatcher]!!
m
Really apologize for miss information, I’ve update my code to use
CoroutineScope
instead. But still compile error because I still don’t know how to provide dependency inside view model like the above
g
Ahhh, I see, you cannot do this like that, you should inject view model into your method
To so that, you need component with view model
m
Do you mean something like this?
Copy code
@DisableInstallInCheck
@Module
interface ViewModelModule {

    @get:[Binds LoginScope IntoMap ViewModelKey(LoginViewModel::class)]
    val LoginViewModel.loginViewModel: ViewModel

    companion object {
        @Provides
        fun provideLoginViewModelScope(viewModel: LoginViewModel): CoroutineScope = viewModel.viewModelScope
    }
}
Unfortunately, the above official docs doesn’t quite work for me (because using DFM). Thus vanilla dagger is needed, that’s why I’m using
@DisableInstallInCheck
in
@Module
configured inside DFM
g
DFM?
m
my apologize, it stands for *D*ynamic *F*eature *M*odule, as stated in android studio official docs for using Hilt in multi-modules here: https://developer.android.com/training/dependency-injection/hilt-multi-module#dfm
g
Anyway, your original snippet is definitely impossible to make work, you cannot magically capture instance value, you need something like official doc, inject VM to this binding Don't know exact details how inject VM, I would recommend to ask on Stackoverflow or report your case on dagger issue tracker
m
noted, really appreciate and a big thanks @gildor