# Converting LiveData to Flow: More lessons learned

DevFeed: [Converting LiveData to Flow: More lessons learned](<https://devfeed.tech/articles/converting-livedata-to-flow-more-lessons-learned-25903.md>)

Original publisher: [Read original article](<https://chao2zhang.medium.com/converting-livedata-to-flow-more-lessons-learned-ba1e068115f5?source=rss-d19045640fe------2>)

Author: Chao Zhang

Published: 2021-06-23T06:52:18Z

Content type: tutorial

Language: en

Sources: [Stories by Chao Zhang on Medium](<https://devfeed.tech/sources/stories-by-chao-zhang-on-medium.md>)

Topics: [kotlin-coroutines](<https://devfeed.tech/topics/kotlin-coroutines.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Android](<https://devfeed.tech/topics/android.md>), [test](<https://devfeed.tech/topics/test.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [cancellation](<https://devfeed.tech/tags/cancellation.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [livedata](<https://devfeed.tech/tags/livedata.md>), [sharedflow](<https://devfeed.tech/tags/sharedflow.md>), [stateflow](<https://devfeed.tech/tags/stateflow.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>)

## AI overview

This continuation explains lessons from converting between Kotlin Flow and LiveData in Android. It covers unfinished hot-flow coroutines during unit tests, why asLiveData cancellation occurs after a timeout on Dispatchers.Main, and how values are replayed when LiveData becomes active again.

## Source excerpt

Photo: Buzz Andersen from Unsplash Android KTX provides opinionated methods facilitating using Kotlin Coroutines in Android Architecture Components. For instance, Flow<T>.asLiveData() is quite helpful to migrate your apps using LiveData towards coroutine. Make sure to understand their behaviors underneath before using them! This story is a continuation of the previous one. Lesson III: Unfinished coroutines during unit testshttps://medium.com/media/41979239402051494c1002bb4d9e57dc/href When converting from Flow to LiveData, the source stream might be a hot StateFlow like above. Running the test will actually end up with the following error: kotlinx.coroutines.test.UncompletedCoroutinesError: Unfinished coroutines during teardown. Ensure all coroutines are completed or cancelled by your test. We know a hot Flow like SharedFlow never completes, so this error appears reasonable. A common way to test hot flow is to cancel the coroutine that the hot flow is executed on. In this case, we may want to pass a custom coroutineContext to asLiveData(context = ...)and cancel the job when the assertions are done: https://medium.com/media/129968c37bf50ac019a25755e3c56411/href However, it does not work! Let's take a step back and consult the documentation of Flow<T>.asLiveData : If the LiveData becomes inactive (LiveData.onInactive) while the flow has not completed, the flow collection will be cancelled after timeoutInMs milliseconds unless the LiveData becomes active again before that timeout (to gracefully handle cases like Activity rotation). Since we are converting a hot flow to LiveData, we will always meet the condition. The flow collection will be canceled after timeoutInMs (default is 5 seconds). After digging it a bit more into the source code, we find that the cancellation is implemented as a delay() call on Dispatchers.Main : https://medium.com/media/154954407331c372f002028c8353a843/href The reason why cancel()runs on Dispatchers.Main is because it is invoked byLiveData.onI