Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to read a value from a kotlin flow?

I have a flow of Episode from a room database. I can observe as livedata this flow with no problem.

But I also would like to read the last value from this flow when the user clicks on a button. I tried with first() terminal flow operator but it does not compile. Can you please help or suggest something else?

The non-compiling attempt to read from a flow:

bd.buttonNext.setOnClickListener {
    lifecycleScope.launch {
        val episode: Episode? = viewModel.episodeFlow().first()           <=== Compile ERROR
        Snackbar.make(bd.root, "episode ${episode?.name}", Snackbar.LENGTH_SHORT).show()
    }
}

This flow comes from ROOM :

@Query("SELECT * FROM Episode WHERE id = :id")
fun getEpisode(id: Long): Flow<Episode?>

Repository:

fun getEpisode(episodeId: Long): Flow<Episode?> = dao.getEpisode(episodeId)

View model - The Id comes from a StateFlow :

fun episodeFlow(): Flow<Episode?> 
  = episodeIdStateFlow.flatMapLatest { episodeId ->
    repository.getEpisode(episodeId)
  }

Compilation error :

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public fun <T> Array<out TypeVariable(T)>.first(): TypeVariable(T) defined in kotlin.collections
public inline fun <T> Array<out TypeVariable(T)>.first(predicate: (TypeVariable(T)) -> Boolean): TypeVariable(T) defined in kotlin.collections
public fun BooleanArray.first(): Boolean defined in kotlin.collections
public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean defined in kotlin.collections
public fun ByteArray.first(): Byte defined in kotlin.collections
public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte defined in kotlin.collections
public fun CharArray.first(): Char defined in kotlin.collections
public inline fun CharArray.first(predicate: (Char) -> Boolean): Char defined in kotlin.collections
public fun CharSequence.first(): Char defined in kotlin.text

As a "workaround", I saved the episode in an instance variable like this but I would like to avoid doing it if possible :

var episode: Episode? = null
...
viewModel.episodeFlow().asLiveData().observe(this) { episode ->
   this.episode = episode
}
...
bd.buttonNext.setOnClickListener {
   Snackbar.make(bd.root, "episode ${episode?.name}", Snackbar.LENGTH_SHORT).show()
}

=================== UPDATE/SOLUTION 21/01/15 ==================

Solution inspired by beigirad (see his post below) using stateIn :

private val _episodeSF  = episodeFlow().stateIn(viewModelScope, SharingStarted.Eagerly, null)
val episodeSF: StateFlow<Episode?>
  get() = _episodeSF

fun episodeFlow(): Flow<Episode?> = episodeIdSF.flatMapLatest { episodeId ->
  repository.episodeFlow(episodeId)
}
like image 746
u2gilles Avatar asked Feb 11 '21 10:02

u2gilles


Video Answer


1 Answers

You can convert your flow to StateFlow by stateIn extension and then use its value attribute to get the latest value.

fun episodeFlow(): StateFlow<Episode?> 
  = episodeIdStateFlow.flatMapLatest { episodeId ->
    repository.getEpisode(episodeId)
  }.stateIn(viewModelScope, SharingStarted.Lazily, initialValue)
like image 122
beigirad Avatar answered Oct 16 '22 11:10

beigirad