I made a coroutine to update the value of a variable, but it doesn’t work:
fun findNameById(id: Long): String {
var name = ""
viewModelScope.launch(Dispatchers.IO) {
val result = async { repository.findName(id) }.await()
name = result
}
return name
}
The variable name
is never updated, it is always an empty string and I don’t understand why. The function findName
works well, I checked it, but the variable name
is never updated. Why?
New contributor
Antonio Caravaggio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.