MutableLiveData: Cannot invoke setValue on a background thread from Coroutine

后端 未结 6 1469
说谎
说谎 2020-12-25 10:27

I\'m trying to trigger an update on LiveData from a coroutine:

object AddressList: MutableLiveData>()
fun getAddressesLiveData(): L         


        
6条回答
  •  情深已故
    2020-12-25 11:22

    I just figured out that it's possible by using withContext(Dispatchers.Main){}:

    object AddressList: MutableLiveData>()
    fun getAddressesLiveData(): LiveData> {
        GlobalScope.launch {
            withContext(Dispatchers.Main){ AddressList.value = getAddressList() }
        }
        return AddressList
    }
    

提交回复
热议问题