How to provide `viewModelScope` as a constructor i...
# android
r
How to provide 
viewModelScope
 as a constructor injection through hilt to other classes which are 
viewmodelscoped
n
Didn't understand what you want to say. You want to provide instantiation of other classes as a dependency to your viewModel? And want them to be viewModel scoped?
r
Nope i want to provide the
viewModelScope
as dependency to other classes which are created by hilt
n
viewModelScope
is just a term to describe the life-cycle of a
viewModel
. How can you provide a dependency of
viewModelScope
?
r
Describe your use case. Looks like what you are trying to do is wrong at the design level.
r
I have a compose screen with a viewmodel and I have few classes which are created by hilt which needs a scope and I want the coroutines created in those classes to be cancelled when the viewmodel is gone That's y I was trying to use the viewmodel scope
r
Does the viewmodel use these classes?
r
@nilTheDev what do u mean it just a term it's a cancellable coroutine scope which is cancelled on clear of viewmodel But u can still create objects in viewmodel which are dependent on viewmodel scope I just want to know if we can do it using hilt
r
If the viewmodel uses these classes you should expose suspend functions.
r
It's not one level of hierarchical classes it's multi level and also some callbacks internally which need the same scope Also https://link.medium.com/N718FKWEdkb article from roman suggest to pass the scope in function
n
Ohh... My bad.... I thought you are talking about the @viewModelScoped annotation that scopes the lifecycle of the dependencies to the viewModel
170 Views