Reputation: 381
I have the following in my viewModel
fun getUsernamePassword(inputUserName:String, inputPassword:String):List<User> = viewModelScope.launch(Dispatchers.IO) {
return repository.getUsernamePassword(inputUserName, inputPassword)
}
However, I am getting an error saying Type mismatch. Required: List<User> Found:Job
What can I do to solve this error. The ultimate goal is to have the function run on a non-black thread operate and a part from the MAIN THREAD to avoid conflict and errors.
Upvotes: 1
Views: 1351
Reputation: 1217
viewModelScope.launch
returns a background Job where you perform your actual repo operation. In this case, you may need an Observable or LiveData to store the list of users in which your UI needs to act upon.
You can do something like this in your ViewModel:
val userList = MutableLiveData<List<User>>()
fun getUsernamePassword(inputUserName:String, inputPassword:String) = viewModelScope.launch(Dispatchers.IO) {
val list = repository.getUsernamePassword(inputUserName, inputPassword)
userList.postValue(list)
}
In your view, you need to observe to changes to the userList
of the ViewModel by:
viewModel.userList.observe(viewLifecycleOwner, {
// Place UI changes here
})
Upvotes: 1