Avatar ·

MutableLiveData setValue error in background thread from coroutine: how to fix?

Hi! I recently started learning Kotlin and Android development, and I encountered a problem when using MutableLiveData with coroutines. I have a ViewModel that loads data from the network using a coroutine. When the data arrives, I try to update the LiveData, but I get a crash with the error: ``` Cannot invoke setValue on a background thread ``` Here is my code: ```kotlin class MyViewModel : ViewModel() { private val _data = MutableLiveData>() val data: LiveData> = _data fun loadData() { viewModelScope.launch { val result = fetchDataFromApi() // suspend function _data.setValue(result) // crashes here } } private suspend fun fetchDataFromApi(): List { delay(2000) /