Skip to content

Instantly share code, notes, and snippets.

View boswelja's full-sized avatar

Jack Boswell boswelja

  • Ceridian HCM
  • New Zealand
View GitHub Profile
@boswelja
boswelja / FlowLifecycleExt.kt
Last active April 15, 2022 08:38
Single-line Flow collector tied to Android's lifecycle
/**
* Observe a Flow on the calling lifecycle. This functions similar to LiveData observe, where
* collectors are tied to the calling scope's lifecycle. Using this, we can avoid having to launch
* a coroutine scope + using repeatOnLifecycle every time.
* @param lifecycleState The lifecycle state to start collecting the Flow on. Collection will be
* cancelled in the mirror state. See [repeatOnLifecycle] for more details.
* @param collector The [FlowCollector] used to collect values. See [Flow.collect] for more details.
*/
context(LifecycleOwner)
fun <T> Flow<T>.observe(