Flow Flashcards

1
Q

What’s Coroutine Flow?

A

It’s a code stream that emits values sequentially, unlike a suspend function, which returns a single value. It is built on suspend functions to produce and consume values asynchronously without blocking the thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Flow Builder used for in Kotlin?

A

Flow Builder is used to create a flow and define how values are produced.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the flow{} builder do in Kotlin?

A

The flow{} builder emits values using the emit() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does flowOf represent in Kotlin?

A

flowOf represents a fixed set of values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does asFlow do in Kotlin?

A

asFlow converts a collection into a flow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of channelFlow in Kotlin?

A

channelFlow emits values concurrently using the send() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a cold flow in Kotlin?

A

A cold flow emits values only when it is collected, and each time a new collector collects the flow, it starts from the beginning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a hot flow in Kotlin?

A

A hot flow emits values even if there are no collectors, and all collectors receive the same values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is StateFlow in Kotlin?

A

StateFlow is a hot flow that retains the latest value and emits updates to its collectors. It requires an initial value, and each new collector receives the most recent value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is SharedFlow in Kotlin?

A

SharedFlow is a hot flow that doesn’t retain the latest value by default, but it can be configured to replay past values to new collectors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a Channel in Kotlin?

A

A Channel is a hot stream that allows coroutines to concurrently send and receive data between each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens when a consumer receives data from a channel in Kotlin?

A

When a consumer receives data from a channel, the data is removed, so other consumers can’t access it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is ChannelFlow in Kotlin?

A

ChannelFlow is a cold stream that allows multiple coroutines to concurrently send and receive data through a Channel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does ChannelFlow combine features in Kotlin?

A

ChannelFlow combines the concurrency of Channel with the asynchronous nature of Flow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the terminal operator in Flow in Kotlin?

A

The terminal operator in Flow triggers it to start emitting data, as Flow is a cold stream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you give some examples of terminal operators in Flow?

A

Examples of terminal operators in Flow include collect(), reduce(), and launchIn().

17
Q

What is flowOn in Kotlin?

A

flowOn is used to change the CoroutineContext of upstream operations in a Flow.

18
Q

How can you run long-running tasks in parallel in Flow?

A

You can use the zip() method to combine two flows and get the results of both tasks in a single callback.

19
Q

What’s the difference between retry and retryWhen in Kotlin Flow?

A

Both retry and retryWhen are used to retry a flow’s execution when an error occurs, but neither can emit new values within the scope.
The key difference is that retryWhen can access the number of attempts to decide whether to retry or not.

20
Q

What is the purpose of debounce in Kotlin Flow?

A

debounce in Flow ensures that a flow emits a new value only if no other value has been emitted within a specific time interval.

21
Q

What does catch do in Kotlin Flow?

A

catch intercepts exceptions that occur during flow execution and allows emitting fallback values.

22
Q

What does onCompletion do in Kotlin Flow?

A

onCompletion is used to perform actions when the flow completes, regardless of whether it was successful or not.
It receives a cause object as a parameter, which determines whether the flow was successful.
The call order is determined by the order in which the actions are placed.

23
Q

Why is launching a coroutine or using withContext invalid in a Flow?

A

It makes the flow non-sequential, and concurrent emissions are prohibited in Flow.

24
Q

What is SharingStarted in Flow?

A

SharingStarted is a principle that defines when the flow starts emitting values.

25
Q

What are the three values for SharingStarted and their behaviors?

A
  • Eagerly: The flow starts emitting values as soon as it is created.
  • Lazily: The flow starts emitting values when the first collector collects.
  • WhileSubscribed: The flow starts emitting values when the first collector collects and stops emitting when no collectors are collecting within a time period.
26
Q

What additional feature does WhileSubscribed in Flow offer?

A

It allows configuring a duration for keeping the replay cache when no collectors are collecting.