Chris Fillmore
10/07/2020, 2:10 AMclass MyViewModel : ViewModel() {
private val users: MutableLiveData<List<User>> by lazy {
MutableLiveData().also {
loadUsers()
}
}
fun getUsers(): LiveData<List<User>> {
return users
}
private fun loadUsers() {
// Do an asynchronous operation to fetch users.
}
}
It is not clear how loadUsers()
could be implemented, as MyViewModel
accepts no dependencies. In particular, if I want to instantiate MyViewModel like so:
val model: MyViewModel by viewModels { SavedStateViewModelFactory(this.application, this) }
it’s not clear to me how I could pass dependencies (like “MySomethingRepository”) to the view modelAfzal Najam
10/09/2020, 5:20 PM