Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Kotlin Flow in MVVM

Using Kotlin Flow in MVVM

LiveData was never designed to be a full-fledged reactive stream. Thread management with LiveData is very hard and making data transformation with transformation operators is very limited. Moreover, testing LiveData becomes very tricky and is likely to open for race conditions if you are using it with coroutines.

Luckily, Kotlin Flow provides asynchronously computed values with the power of Coroutines. Making data transformations becomes very easy with Flow intermediate operators. Furthermore, StateFlow provides caching the current state just like LiveData. Since there is a back-pressure in Flow, unit testing is much simpler with handy Flow terminal operators.

Kotlin Flow is a very powerful reactive stream with its robust operators. Using Flow instead of LiveData makes it easier to handle data transformations and background processes. Finally, testing Flow is much safer and painless compared to LiveData.

Fatih Giriş

October 21, 2021
Tweet

More Decks by Fatih Giriş

Other Decks in Programming

Transcript

  1. LiveData in MVVM • LiveData - Backpressure ❌ Producer Observer

    Ref: https://github.com/Froussios/Intro-To-RxJava/blob/master/Part%204%20-%20Concurrency/4.%20Backpressure.md
  2. LiveData in MVVM • LiveData - Backpressure ❌ - Data

    transformation ❌ - Requires lifecycle owner to observe (Activity, Fragment …) 🤔
  3. LiveData in MVVM • ViewModel: LiveData • Repository: Suspend Ref:

    https://developer.android.com/jetpack/guide
  4. LiveData in MVVM Open Twitter app Get the tweets from

    cache Fetch new tweets from server Update UI
  5. LiveData in MVVM Open Twitter app Get the tweets from

    cache Fetch new tweets from server Update UI
  6. LiveData + Flow • ViewModel: LiveData • Repository: Flow Ref:

    https://developer.android.com/jetpack/guide
  7. Testing • LiveData • LiveData & Coroutines might result some

    race condition • Need to observe again when Transformations used
  8. TL;DR • Flow & LiveData similar • One shot request

    • Repository: Suspend or Flow • ViewModel: LiveData or Flow (with repeatOnLifecycle in the View layer) • Data stream • Repository: Flow • ViewModel: LiveData or Flow (with repeatOnLifecycle in the View layer) • LiveData Tests 😥 - Flow Tests 😎
  9. References • https://proandroiddev.com/using-livedata-flow-in-mvvm-part-i- a98fe06077a0 • https://proandroiddev.com/using-livedata-flow-in-mvvm-part- ii-252ec15cc93a • https://github.com/fgiris/LiveDataWithFlowSample •

    https://developer.android.com/topic/libraries/architecture/livedata • https://proandroiddev.com/when-not-to-use-livedata-6a1245b054a6 • https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/ kotlinx.coroutines.flow/index.html
  10. References • https://medium.com/@elizarov/kotlin-flows-and- coroutines-256260fb3bdb • https://github.com/Kotlin/kotlinx.coroutines/pull/1354 • https://codelabs.developers.google.com/codelabs/ advanced-kotlin-coroutines/ •

    https://medium.com/androiddevelopers/unit-testing-livedata- and-other-common-observability-problems-bb477262eb04 • https://medium.com/swlh/kotlin-coroutines-in-android-unit- test-28ff280fc0d5