Asynchronous Data Loading With New Kotlin Flow

Yet another amazing operator, Flow, from coroutines in Kotlin

Siva Ganesh Kantamani
Jan 3 · 6 min read

Coroutines

When we call an asynchronous function like a service call, retrieving data from the database, reading files or anything, we need a callback so that we know that the operation is finished and we can resume the actual work like in mobiles updating the UI after receiving data from your servers.

Simple Coroutine sample

Suspend Functions

A suspending function is simply a function that can be paused and resumed at a later time. They can execute a long-running operation and wait for it to complete without blocking service calls, database operations, or reading a long file.

suspend fun getItem() : Response
suspend fun getItems() : List<Response>
suspend fun foo() : List<Responses> = buildlist{
add(Execute("A"))
add(Execute("B"))
add(Execute("C"))
}
fun getData() {
val list = withContext(Dispatchers.IO) { foo() }
for (x in list) println(x)
}
Executing list of request with suspend function

Channel

Channels are pipes, structured, where you send data on one side and receive data on the other side, as shown below.

Overview of how channels work
suspend fun foo() : ReceiveChannel<Response> = produce{
send(execute("A"))
send(xecute("B"))
send(Execute("C"))
}
fun getData() {
val chaannel = withContext(Dispatchers.IO) { foo() }
for (x in channel) println(x)
}
channels work flow

Kotlin Flow

The stable version of Flow was released a few days back. Flow not only solves the pain points of Channels but also offers many new features which you’ll see in this article.

fun foo() : Flow<Response> = flow{
emit(execute("A"))
emit(execute("B"))
emit(execute("C"))
}
fun getData() {
val flowData = foo()
withContext(Dispatchers.IO) { flowdata.collect{ println(x) }
}
Flow Execution

Flow Is Declarative

When we call the foo function in the flow sample, what happens is that it create a flow and returns it, so we can use some operators like map to make flow more declarative, as shown below.

fun foo() : Flow<Response> = 
flowOf("A","B","C").map{ name->
execute(name)
}
}

Flow Is Reactive

By reading this headline, many of the developers think of RxJava. Yes, RxJava is a kind of start to a reactive program in JVM where Kotlin runs mainly.

Why Flow?

Let’s take a close look, for example, if you want to transform any object of type A to type B in RxJava, we have an operator called map.

Better Programming

Advice for programmers.

Siva Ganesh Kantamani

Written by

Android lead at Brandpier Inc based in Hyderabad, Traveler, Blogger, Dreamer. Find me at about.me/sivaganesh_kantamani and on Twitter @SG5202.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade