DEV Community

Cover image for callbackFlow - Callback to Flow API in Kotlin
Amit Shekhar
Amit Shekhar

Posted on • Edited on • Originally published at outcomeschool.com

callbackFlow - Callback to Flow API in Kotlin

I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

In this blog, we will learn how to convert any Callback to Flow API in Kotlin using callbackFlow.

This article was originally published at Outcome School.

This blog is a part of the series I have written on Flow API in Kotlin:

We use many libraries in our Android Project that provides the callback way to use instead of the Flow API way. As nowadays, we all have started using Kotlin Coroutines Flow API in our projects, so it becomes our responsibility to implement things in a way that supports Flow API.

So, we need to learn how to convert any Callback to Flow API in Kotlin using callbackFlow. And, this is the topic of this blog.

Here, I will take an example of a LocationManager just for the sake of understanding.

Assume that we can use LocationManager and listen to the changes in location as below:

// create a location listener
val locationListener = object : LocationListener {

    override fun onLocationUpdate(location: Location) {
        // do something with the updated location
    }

}

// register for location updates
LocationManager.registerForLocation(locationListener)

// unregister in onDestroy()
LocationManager.unregisterForLocation(locationListener)
Enter fullscreen mode Exit fullscreen mode

Here, we have a LocationListener through which we get the onLocationUpdate callback.

Now, we want to use this LocationManager in the Flow API way.

Let's do this by creating a function which returns Flow<Location> as below:

fun getLocationFlow(): Flow<Location> {
    return callbackFlow {

        val locationListener = object : LocationListener {

            override fun onLocationUpdate(location: Location) {
                trySend(location)
            }

        }

        LocationManager.registerForLocation(locationListener)

        awaitClose {
            LocationManager.unregisterForLocation(locationListener)
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Now, it's time to understand what we have done to convert the Callback to Flow API.

We have followed the following steps to convert the Callback to Flow API in Kotlin:

  • Create a function to return the Flow<Location>.
  • Use callbackFlow as the return block.
  • Use trySend() for the location updates.
  • Use awaitClose block to unregister.

Now, we can use the above function as below:

launch {
    getLocationFlow()
    .collect { location ->
        // do something with the updated location
    }
}
Enter fullscreen mode Exit fullscreen mode

This is how we can convert any callback to Flow API in Kotlin using callbackFlow.

So, today we learned how to convert any callback to Flow API and use them.

Master Kotlin Coroutines from here: Mastering Kotlin Coroutines

That's it for now.

Thanks

Amit Shekhar

Co-Founder @ Outcome School

You can connect with me on:

Read all of my high-quality blogs here.

Top comments (0)