Coroutines with LiveData using Flow(Part III)
We have gone through the Coroutines with LiveData, its implementation and basics of Flow in coroutines in Part II. If you haven’t gone through (Part II) ,please find it from here: Fastlane to Coroutines: Working with LiveData and Flow(Part II)
In the previous article, we had started going through the Flow
and I could say it was just a glimpse of the movie but now I’m not gone release trailer rather I’m releasing the whole movie with interesting stuff to learn and enjoy as an advance coder.
We have already understood the async with Flow
in the previous article. Now let’s understand how we could use Flow
with Room.
Using Flow with Room :
In the example below, We get the list of articles from the database but with Flow
return type. we’ll use the asLiveData
extension function to convert Flow
into a LiveData
. Just like the LiveData
builder, this adds a configurable timeout to the LiveData
generated. We observe LiveData
the same as we do and serve to UI.
Combine multiple flows:
Supposing we have two flows and we have the requirement to use both the logics then we need to combine both the flows. Let’s understand this scenario.
In the example below, we have sort order flow for sorting and we need sorted articles list then we combine both the flows using combine
operator. Both the flows will run on different coroutines concurrently.
Sometimes functionality may become slow enough to block main thread then Flow
offers a declarative API called flowOn
to control which thread the flow runs on.
In the example below, A new coroutine is launched on the defaultDispatcher
to run and collect the flow before the call to flowOn
.we would only care about the most recent result. That’s what the conflate
operator does. it modifies the buffer of flowOn
to store only the last result. If another result comes in before the previous one is read, it gets overwritten.
Switching between two flows :
In the example below, switching between the flows is shown which does the same thing as the LiveData.switchMap
which works for switching between two data sources based on an event.
We have usedConflatedBroadcastChannel
which is a special kind of coroutine-based value holder that broadcasts the last value and lets you observe it. It works the same as LiveData
but it holds the last element. However, unlike LiveData
, you have to use coroutines to read values on multiple threads.
Flow’s flatMapLatest
extensions allow you to switch between multiple flows same as switchMap
from LiveData.
And finally, we convert the Flow
into a LiveData
to observe in our UI.
Flow provides some suspending functions to orchestrate async work because sometimes we have to sort or change before serving to UI.
In the example below, We have applied map transform for custom sorting.
We are almost at our final step which is optional but needs to be understood because it will require in our ViewModel.
Network request into Flow-based coroutines :
If you want to move network request into flow-based coroutine in your ViewModel then let’s see our final step.
In the example below,onCompletion
will be called every time the flow above it completes. It's the same thing as a finally block as its name suggests and for the catch
operator I’m not gonna say anything because our coders are advance that’s why they are here.
Conclusion:
Hopefully, our coders would have got the idea for LiveData
and Flow
with coroutines. If we want structure concurrency in a sequential manner with our love “async” work then we have better options as coroutines to adventure in our next project.
If you find this article helpful then please give your valuable claps with feedback and motivate me.
Happy Coding.