Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using map on Kotlin's Stateflow

With LiveData inside a Viewmodel we use switchMap or Transformations.map like this

val recipesList = cuisineType.switchMap { repository.getDisplayRecipes(it.cuisineType).asLiveData() }

What would be the best way to do this with StateFlow? I know we can just use map like below, however this this would return me Flow<Flow<List< Recipe>>> which doesn't seem correct

val recipeListFlow = cuisineTypeStateFlow.map {
    repository.getDisplayRecipes(it.cuisineType)
}
like image 900
alfietap Avatar asked Feb 07 '21 20:02

alfietap


People also ask

Is StateFlow hot or cold?

StateFlow is a hot flow—it remains in memory as long as the flow is collected or while any other references to it exist from a garbage collection root.

What is the difference between SharedFlow and StateFlow?

The main difference between a SharedFlow and a StateFlow is that a StateFlow takes a default value through the constructor and emits it immediately when someone starts collecting, while a SharedFlow takes no value and emits nothing by default.

How do I update my mutable state flow?

A mutable state flow is created using MutableStateFlow(value) constructor function with the initial value. The value of mutable state flow can be updated by setting its value property. Updates to the value are always conflated. So a slow collector skips fast updates, but always collects the most recently emitted value.

What is Stateflow in Kotlin?

StateFlow is like a way to use Kotlin Flow to manage and represent a state in an application. StateFlow is a type of interface, which is only a read-only and always returns the updated value. And to receive the updated value we just collect the value from the implemented Flow.

What is a map in Kotlin?

Kotlin map is a collection of key/value pairs, where each key is unique, and it can only be associated with one value. The same value can be associated with multiple keys though. We can declare the keys and values to be any type; there are no restrictions. A Kotlin map can be either mutable ( mutableMapOf) or read-only ( mapOf ).

How do Kotlin’s flow operators work?

If you are not familiar with Kotlin’s Flow operators, they work pretty much exactly like the standard Collection operators from a user perspective, except that they are performed on a Flow and they transform to a Flow. Below you can see a comparison between map on a Collection (technically on an Iterable) and on a Flow.

What is the difference between map and maplatest in Stateflow?

Since a StateFlow is a type of Flow, we have all of the Flow operators available to us. In this case we will use a variation on map called mapLatest. mapLatest works like map except that it cancels any pending transform operations. So if the Flow emits 100 values, map will map every value, while mapLatest will only map the terminal value.


Video Answer


1 Answers

Should be

val recipeListFlow = cuisineTypeStateFlow.flatMapLatest {
    repository.getDisplayRecipes(it.cuisineType)
}
like image 69
EpicPandaForce Avatar answered Oct 23 '22 18:10

EpicPandaForce