Fastlane to Coroutines: Working with LiveData and Flow(Part II)

Devang Chauhan
Mindful Engineering
3 min readOct 17, 2020
Photo by Adrien Converse on Unsplash

We have already discussed the nitty-gritty of Coroutines basics, usages and its implementations. If you haven’t gone through (Part I), please find it from here: Fast Lane to Coroutines.

In this article we are going to use LiveData with Coroutines and We will use Flow for asynchronous operations. This article has a prerequisite that you have an understanding of Architecture Components like ViewModel, LiveData and Room. and of course if you haven’t gone through our first article of this series then please go through it.

Let’s begin.

Coroutines with Kotlin-Friendly LiveData :

Coroutines and LiveData

When using LiveData, you might need to calculate values asynchronously. For example, you might want to retrieve a list of article items and serve them to the UI. In these cases, you can use the liveData builder function to call a suspend function, serving the result as a LiveData object.

In the example below, fetchUser() is a suspend function declared elsewhere. Use the liveData builder function to call fetchUser() asynchronously, and then use emit() to emit the result:

The liveData building block makes concurrency between coroutines and LiveData. LiveData is cancelled automatically after a configurable timeout if it is inactive and becomes active again only if it is cancelled automatically .

Modifying values for LiveData :

You can also combine liveData with Transformations. In the example below,switchMap function is used to sort the article list got as liveData and emit the sorted list as liveData.

Multiple values from LiveData :

You can emit multiple values from a LiveData by calling the emitSource() function whenever you want to emit a new value. Note that each call toemit() or emitSource() removes the previously-added source.
In the example below, We get an article from the database and emitted (Result.loading(it)) it, then we fetch an article from the server and save it in the database. This saved article is emitted as success value(Result.success(it)) and emitted as an error value(Result.error(it)) if data is not saved.

Use of Flow in Coroutines:

Flow is an asynchronous version of a Sequence. Just like a sequence, a Flow produces each value on-demand whenever the value is needed, and flows can contain an infinite number of values.

So we would have a question that Why did kotlin introduce Flow and How is it different from regular Sequence?

Well… the answer is “Async” — developer’s first love.
Yes.Flow includes full support for coroutines. That means you can build, transform, and consume Flow using coroutines. You can also control concurrency, which means coordinating the execution of several coroutines declaratively with Flow.

Let us first understand how Flow works.

In the example below, The Flow starts running when the collect operator runs. The suspending operator collect is called a terminal operator in Flow. There are other suspending terminal operators such as toList, first and single shipped with kotlinx-coroutines, and you can build your own. In the example, all the emits will be sequential so we could manage async behaviour in a sequential manner.

Conclusion

In this blog we went through LiveData and basics of Flow. For not making it longer we’ll get details of Flow in the next blog.

Happy Coding.

--

--