How to use coroutine LiveData in Android
Issue #358
app/build.gradle
1 | implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01" |
1 | import androidx.lifecycle.ViewModel |
Use coroutines with LiveData
https://developer.android.com/topic/libraries/architecture/coroutines
The liveData building block serves as a structured concurrency primitive between coroutines and LiveData. The code block starts executing when LiveData becomes active and is automatically canceled after a configurable timeout when the LiveData becomes inactive.
Source code
https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/lifecycle/lifecycle-livedata-ktx/src/main/java/androidx/lifecycle/CoroutineLiveData.kt
https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/lifecycle/lifecycle-livedata-ktx/src/main/java/androidx/lifecycle/FlowLiveData.kt
CoroutineLiveData.kt
1 |
|
a LiveData that tries to load the
User
from local cache first and then tries from the server and also yields the updated value
1 | val user = liveData { |