Differences in Methods Of Collecting Kotlin Flows

Differences in Methods Of Collecting Kotlin Flows

Some of you may have recently started using Kotlin Flow, the new framework by JetBrains to handle observable streams.

Some of you may have recently started using Kotlin Flow, the new framework by JetBrains to handle observable streams.

As you probably already know from many articles on the internet, in order to collect a flow, you have two main options. You can either use .collect() , or .launchIn() . (For the purposes of this article we avoid other terminal operators such as .toList(), etc)

What are the differences between these two methods?

There are some obvious ones and one is very subtle, not immediately obvious to everyone and you should pay attention to it, if you want your code to behave as expected.

Obvious differences

For one, if we take a look at method signatures of these two methods, we’ll learn, that .collect() is suspending method, while .launchIn() is not:

public suspend fun collect(collector: FlowCollector<T>)

public fun <T> Flow<T>.launchIn(scope: CoroutineScope): Job

The practical difference then is, that you can call collect() method only from another suspending function or from a coroutine. For example like this:

coroutineScope.launch {
    flowOf(1, 2, 3)
        .collect { println(it) }
}

whereas .launchIn() can be called like this in any regular function:

flowOf(1, 2, 3)
    .onEach { println(it) }
    .launchIn(coroutineScope)

With the .launchIn() method, you also get a Job as return value, so you could cancel the flow by cancelling the job:

val job = flowOf(1, 2, 3)
    .onEach { println(it) }
    .launchIn(coroutineScope)

job.cancel()

There’s one more difference between these two methods, that may not be so obvious however and it returns to the fact, that .collect() is a suspending function.

kotlin kotlin-flow kotlin-coroutines

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Kotlin Coroutines on Android - How to use Coroutines on Android

Coroutines are a Kotlin feature that convert async callbacks for long-running tasks, such as database or network access, into sequential code. This Kotlin Coroutines tutorial will show you how to use coroutines on Android, and how the new androidx-concurrent library makes it easy to use them to get things off the main thread. You'll also learn how the new library helps coroutines work with Architecture Components. This session also covers coroutine patterns, best practices, and even how to test coroutines!

Callback Flows in Android

Kotlin Flows are amazing. Not only do they provide a non-blocking asynchronous way to deal with sequences of values, but they provide a clean and powerful API for dealing with event driven programming. Flows are easy to clean up to prevent leaks.

Kotlin Coroutines-Async/Await

In the previous article, we learned about suspended functions provided by coroutines and the value they bring to the code. We also learned how coroutine and suspended functions work hand in hand to bridge threads and execute asynchronous tasks without us having to create notification mechanisms.

How to Develop an Android App with Kotlin?

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

What is Flow Protocol (FLOW) | What is Flow Protocol token | What is FLOW token

Flow is a protocol that establishes an Ethereum based self-distributing store of value token (FLOW). FLOW is designed to be a hedge against Bitcoin as today’s primary digital store of value. The protocol distributes inflation of FLOW tokens to all addresses holding it without the need for a single transaction. Inflation happens daily and does not require any action on the side of the FLOW token holder or any other parties. This method of inflation allows for the token to be applied to DeFi use cases without diluting the token holders. What is Flow Protocol (FLOW) | What is Flow Protocol token | What is FLOW token