Suspend Functions vs. Flows In Kotlin & Android
Summary
TLDRIn this video, the presenter explains when to use Kotlin coroutines' `suspend` functions versus `flows`, with a focus on handling API calls. While `suspend` functions are ideal for single-shot responses, `flows` are better suited for cases requiring multiple emissions over time, such as with a database or a timer. The video also emphasizes the importance of choosing the right approach for managing loading states and clarifies common misunderstandings regarding flow usage. Additionally, it offers insights into Kotlin Conf, an event for developers passionate about Kotlin, hosted by JetBrains.
Takeaways
- 😀 **Suspend functions** are ideal for single-result operations like API calls that return one response.
- 😀 **Flow** is used when you need to emit multiple results over time, such as streaming data or database updates.
- 😀 A **suspend function** is a one-time operation, and once the result is returned, the function ends.
- 😀 Flows allow you to emit multiple values, which makes them perfect for dynamic data like real-time updates or a timer.
- 😀 Using **Flow** for API calls that return a single result is usually overkill and can create unnecessary complexity.
- 😀 The **Flow** approach is beneficial for scenarios where data may change over time, such as monitoring changes in a Room database.
- 😀 Using a loading state (like `Result.Loading`) inside a **Flow** for a simple API call can confuse the code's intention and is often not needed.
- 😀 In the UI layer, loading states should be managed separately, typically before or after the API call, not embedded inside the data layer.
- 😀 Flows should be used for cases like database streaming, where real-time data changes need to be handled.
- 😀 **KotlinConf 2026** in Munich offers a chance to learn from experts at JetBrains and major tech companies about Kotlin best practices.
- 😀 Using a `flow` to handle tasks like timers or database changes is a great alternative to using suspend functions, especially for continuous operations.
Q & A
What is the main topic of the video?
-The video discusses when to use `suspend` functions and when to use `flows` in Kotlin coroutines, particularly in the context of making API calls.
What is the primary difference between a suspend function and a flow?
-A suspend function is used for single-shot function calls, returning a single result. A flow, on the other hand, is used for emitting multiple values over time, and is suitable for operations that might produce multiple results, such as continuous data updates.
When should you use a suspend function instead of a flow?
-You should use a suspend function for API calls where only a single result is expected, such as when making a network request that returns a single response. Flows are more appropriate for cases where you expect multiple values over time.
Can a flow be used without being a suspend function? How?
-Yes, a flow can be used without being a suspend function. Flows can emit suspending values within their builders because flows themselves are based on coroutines, which allows them to suspend execution while waiting for results.
What is the purpose of `emit` in the flow example?
-The `emit` function is used to send a new value into the flow. In the context of the video, it’s used to emit the result of an API call, such as success or failure.
What is the issue with using loading states in a result class?
-The issue with using a loading state in a result class is that loading is not inherently a result. It can be misleading because it suggests that the flow might emit multiple results when it’s only meant to represent the loading state, which can be better handled by the UI layer.
What are some scenarios where using a flow is more appropriate than a suspend function?
-Flows are ideal when working with continuous data updates, such as changes in a database (e.g., Room), or when you need to emit values over time, like in a timer. These use cases require a flow to handle multiple emissions.
What is the advantage of using a flow for a database table in Room?
-A flow can provide real-time updates when the database table changes. Unlike a suspend function, which only retrieves a snapshot of the current state, a flow will emit new values as changes occur in the database, allowing the UI to be updated with the most recent data.
What’s the benefit of using `onEach` for collecting flow emissions?
-Using `onEach` for collecting flow emissions allows you to handle each emitted value in real-time. This can be useful for updating the UI based on each emitted state, such as showing loading, success, or error statuses as the flow progresses.
How does the flow-based approach compare to the suspending approach in terms of UI updates?
-The flow-based approach allows for multiple emissions over time, enabling the UI to react to intermediate states (e.g., loading, success, error). In contrast, the suspend function approach typically only updates the UI once after the function completes, making it less suitable for continuous updates.
Outlines

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowMindmap

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowKeywords

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowHighlights

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowTranscripts

This section is available to paid users only. Please upgrade to access this part.
Upgrade NowBrowse More Related Video

5 Kotlin Coroutine Secrets I Wish I Knew Earlier

Make a Weather App for Android | Android Studio | Kotlin

Kotlin in 100 Seconds

Chegou a Vez do Kotlin em 2024? (Análise da Linguagem ao Mercado)

So Many New Kotlin & Compose Features 😱 - Mobile Dev News September 2025

Ambiguity Problem and its Solution with Constructor Injection | Spring Framework Tutorial
5.0 / 5 (0 votes)