Slide 1

Slide 1 text

Using Kotlin Flow in MVVM S. Fatih Giris Android Lead @ DNB Bank fgiris fatih_grs

Slide 2

Slide 2 text

Sample https://github.com/fgiris/LiveDataWithFlowSample

Slide 3

Slide 3 text

Agenda • LiveData in MVVM • LiveData + Flow • Only Flow • Testing

Slide 4

Slide 4 text

LiveData in MVVM • LiveData ✓Lifecycle aware ✓No memory leak ✓Cache ✓…

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

LiveData in MVVM • LiveData - Backpressure ❌ - Data transformation 🥵

Slide 7

Slide 7 text

LiveData in MVVM • LiveData - Backpressure ❌ - Data transformation ❌ - Requires lifecycle owner to observe (Activity, Fragment …) 🤔

Slide 8

Slide 8 text

LiveData in MVVM • Weather forecast app • One-shot request • Data stream

Slide 9

Slide 9 text

LiveData in MVVM • ViewModel: LiveData • Repository: Suspend Ref: https://developer.android.com/jetpack/guide

Slide 10

Slide 10 text

LiveData in MVVM WeatherForecastRepository.kt

Slide 11

Slide 11 text

LiveData in MVVM WeatherForecastRepository.kt

Slide 12

Slide 12 text

LiveData in MVVM WeatherForecastViewModel.kt

Slide 13

Slide 13 text

LiveData in MVVM WeatherForecastViewModel.kt

Slide 14

Slide 14 text

LiveData in MVVM WeatherForecastViewModel.kt

Slide 15

Slide 15 text

LiveData in MVVM WeatherForecastViewModel.kt

Slide 16

Slide 16 text

LiveData in MVVM WeatherForecastViewModel.kt

Slide 17

Slide 17 text

Return multiple values?

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

LiveData in MVVM Open Twitter app

Slide 20

Slide 20 text

LiveData in MVVM Open Twitter app Get the tweets from cache

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

LiveData in MVVM WeatherForecastRepository.kt

Slide 24

Slide 24 text

LiveData in MVVM WeatherForecastRepository.kt

Slide 25

Slide 25 text

Flow 🤩

Slide 26

Slide 26 text

Flow • Reactive stream which can return asynchronously computed values

Slide 27

Slide 27 text

Flow Pros • Structured concurrency • Cold stream • Efficient data transformation • Easy testing

Slide 28

Slide 28 text

LiveData + Flow • ViewModel: LiveData • Repository: Flow Ref: https://developer.android.com/jetpack/guide

Slide 29

Slide 29 text

LiveData + Flow WeatherForecastRepository.kt

Slide 30

Slide 30 text

LiveData + Flow WeatherForecastRepository.kt

Slide 31

Slide 31 text

LiveData + Flow WeatherForecastOneShotViewModel.kt

Slide 32

Slide 32 text

LiveData + Flow WeatherForecastOneShotViewModel.kt Flow

Slide 33

Slide 33 text

LiveData + Flow WeatherForecastOneShotViewModel.kt Flow

Slide 34

Slide 34 text

LiveData + Flow WeatherForecastOneShotFragment.kt

Slide 35

Slide 35 text

LiveData + Flow WeatherForecastRepository.kt

Slide 36

Slide 36 text

LiveData + Flow WeatherForecastRepository.kt Data per second

Slide 37

Slide 37 text

LiveData + Flow WeatherForecastDataStreamViewModel.kt

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Flow Intermediate Flow Operators • map • filter • transform • onEach • …

Slide 40

Slide 40 text

LiveData + Flow map operator

Slide 41

Slide 41 text

LiveData + Flow map operator

Slide 42

Slide 42 text

LiveData + Flow map operator

Slide 43

Slide 43 text

LiveData + Flow onStart operator

Slide 44

Slide 44 text

LiveData + Flow filter operator

Slide 45

Slide 45 text

LiveData + Flow onEach operator

Slide 46

Slide 46 text

LiveData + Flow flowOn operator

Slide 47

Slide 47 text

LiveData + Flow catch operator

Slide 48

Slide 48 text

LiveData + Flow asLiveData extension

Slide 49

Slide 49 text

Why not only Flow?

Slide 50

Slide 50 text

LiveData 👋

Slide 51

Slide 51 text

Flow • Cold stream • ≠ LiveData • Configuration changes 🙄

Slide 52

Slide 52 text

Flow WeatherForecastDataStreamFlowViewModel.kt 😭

Slide 53

Slide 53 text

Flow WeatherForecastDataStreamFlowViewModel.kt No asLiveData

Slide 54

Slide 54 text

Flow WeatherForecastDataStreamFlowFragment.kt

Slide 55

Slide 55 text

Flow WeatherForecastDataStreamFlowFragment.kt Terminal Operator 👈

Slide 56

Slide 56 text

Flow WeatherForecastDataStreamFlowFragment.kt

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Testing🚦

Slide 59

Slide 59 text

Testing • LiveData • LiveData & Coroutines might result some race condition • Need to observe again when Transformations used

Slide 60

Slide 60 text

Testing • Flow • Sequential • No race condition • Test like a list (toList )

Slide 61

Slide 61 text

Testing WeatherForecastRepository.kt

Slide 62

Slide 62 text

Testing WeatherForecastRepositoryTest.kt

Slide 63

Slide 63 text

Testing WeatherForecastRepositoryTest.kt

Slide 64

Slide 64 text

Testing WeatherForecastRepositoryTest.kt

Slide 65

Slide 65 text

Testing WeatherForecastRepositoryTest.kt

Slide 66

Slide 66 text

Testing WeatherForecastRepositoryTest.kt

Slide 67

Slide 67 text

Testing WeatherForecastRepositoryTest.kt

Slide 68

Slide 68 text

Testing WeatherForecastRepositoryTest.kt

Slide 69

Slide 69 text

Testing Dispatchers.Main -> Android Main Looper -> Android OS

Slide 70

Slide 70 text

Testing Solution: TestCoroutineDispatcher

Slide 71

Slide 71 text

Testing MainCoroutineRule.kt

Slide 72

Slide 72 text

Testing MainCoroutineRule.kt

Slide 73

Slide 73 text

Testing MainCoroutineRule.kt

Slide 74

Slide 74 text

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 😎

Slide 75

Slide 75 text

https://medium.com/@girisfth Missed something?

Slide 76

Slide 76 text

QUESTIONS @fatih_grs

Slide 77

Slide 77 text

Stay safe!

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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