简体   繁体   中英

Android Livedata Observer Coroutine Kotlin

Is it possible to have a coroutine inside an observer to update the UI

For Example:

Viewmodel.data.observer(this, Observer{ coroutinescope })

You can run any code you want from the Observer callback. But it would not be a good idea to launch a coroutine that updates the UI later, because when the coroutine is complete, the UI might be destroyed, which can cause exceptions to be thrown and crash your app.

Just run the UI update code directly from the Observer callback.

viewModel.data.observe(this, Observer {
    // Update the UI here directly
})

That way you know that UI is alive when you update it, since LiveData takes the lifecycle of this into account.

If you want to launch some coroutine at the same time as the callback, it would be better to do it within your viewModel using viewModelScope .

// This triggers the above code
data.value = "foo"

// Now also launch a network request with a coroutine
viewModelScope.launch { 
    val moreData = api.doNetworkRequest()
    // Set the result in another LiveData
    otherLiveData.value = moreData
}

Note that you must add a dependency to build.gradle in order to use viewModelScope :

dependencies {
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
}

Yes, it's possible. You can launch a GlobalScope coroutine and when you need update the UI you should be that in activity.!.runOnUiThread

Here a sample.

viewModel.phoneNumber.observe(this, Observer { phoneNumber ->
        GlobalScope.launch {
            delay(1000L) //Wait a moment (1 Second)
            activity!!.runOnUiThread {
                binding.textviewPhoneLabel.edittextName.setText(phoneNumber)
            }
        }
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM