Slide 1

Slide 1 text

Jossi Wolf @jossiwolf From LiveData to Coroutines Flow

Slide 2

Slide 2 text

@jossiwolf @jossiwolf

Slide 3

Slide 3 text

@jossiwolf Live Data, LiveData everywhere

Slide 4

Slide 4 text

@jossiwolf ªġƛŸƦŊƴŸƞǛ ßŊġǕwŸĚġŢ mŊǔġ(öƴö ÇXmöǛġƞ XŭǔŸŞġƦ mŊǔġ(öƴö XŭǔŸŞġƦ

Slide 5

Slide 5 text

@jossiwolf ”LiveData is an observable data holder class” - Android Developers Documentation

Slide 6

Slide 6 text

@jossiwolf class HomeViewModel { val latestData: LiveData = MutableLiveData() } LiveData - an example

Slide 7

Slide 7 text

@jossiwolf class HomeViewModel { val latestData: LiveData = MutableLiveData() fun refresh() { val data = fetchData() latestData.post(data) } } LiveData - an example

Slide 8

Slide 8 text

@jossiwolf class HomeFragment: Fragment() { fun setupUI() { latestData.observe(viewLifecycleOwner) { data #-> ##... } } } LiveData - an example

Slide 9

Slide 9 text

@jossiwolf # LiveData is great! *when used properly

Slide 10

Slide 10 text

@jossiwolf # LiveData & Execution Context

Slide 11

Slide 11 text

@jossiwolf class SupermarketRepository { fun fetchOpeningHours( id: SupermarketId ): LiveData = ##... } Our Repository

Slide 12

Slide 12 text

@jossiwolf class SupermarketRepository { fun fetchOpeningHours( id: SupermarketId ): LiveData = ##... } Our Repository

Slide 13

Slide 13 text

@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel

Slide 14

Slide 14 text

@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel

Slide 15

Slide 15 text

@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel

Slide 16

Slide 16 text

@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } }

Slide 17

Slide 17 text

@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } }

Slide 18

Slide 18 text

@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } }

Slide 19

Slide 19 text

@jossiwolf LiveData map( LiveData source, Function mapFunction ) { MediatorLiveData result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map

Slide 20

Slide 20 text

@jossiwolf LiveData map( LiveData source, Function mapFunction ) { MediatorLiveData result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map

Slide 21

Slide 21 text

@jossiwolf LiveData map( LiveData source, Function mapFunction ) { MediatorLiveData result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map

Slide 22

Slide 22 text

@jossiwolf LiveData map( LiveData source, Function mapFunction ) { MediatorLiveData result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map

Slide 23

Slide 23 text

@jossiwolf LiveData map( LiveData source, Function mapFunction ) { MediatorLiveData result = new MediatorLiveData#<>(); result.addSource(source, (x) #-> { result.setValue(mapFunction.apply(x)); }); return result; } cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:lifecycle/lifecycle-livedata/src/main/java/androidx/lifecycle/Transformations.java androidx.lifecycle.Transformations#map

Slide 24

Slide 24 text

@jossiwolf ”The events are dispatched on the main thread.” - Android Developers Documentation

Slide 25

Slide 25 text

@jossiwolf # LiveData Observers are always called on the main thread

Slide 26

Slide 26 text

@jossiwolf # It’s easy to let things slide when using LiveData

Slide 27

Slide 27 text

@jossiwolf Replace LiveData Make sure threading is done properly

Slide 28

Slide 28 text

@jossiwolf Replace LiveData @jossiwolf

Slide 29

Slide 29 text

@jossiwolf One-Shot Streams of Data

Slide 30

Slide 30 text

@jossiwolf Coroutines! One-Shot Streams of Data

Slide 31

Slide 31 text

@jossiwolf Coroutines! Flow One-Shot Streams of Data

Slide 32

Slide 32 text

@jossiwolf # Converting to Coroutines

Slide 33

Slide 33 text

@jossiwolf fun fetchOpeningHours(id: SupermarketId): LiveData { val liveData = MutableLiveData() val openingHours = poiService.downloadOpeningHours(poiType = SUPERMARKET, id).enqueue( object: Callback() { override fun onResponse(call: Call, response: Response) { liveData.postValue(response.body()) } ##... } ) liveData.postValue(openingHours) return liveData } Repository with LiveData + Callbacks

Slide 34

Slide 34 text

@jossiwolf suspend fun fetchOpeningHours(id: SupermarketId): LiveData { val liveData = MutableLiveData() val openingHours = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) liveData.postValue(openingHours) return liveData } Repository with LiveData + Coroutines

Slide 35

Slide 35 text

@jossiwolf suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) That method as Coroutine…

Slide 36

Slide 36 text

@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) }

Slide 37

Slide 37 text

@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) }

Slide 38

Slide 38 text

@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) }

Slide 39

Slide 39 text

@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) }

Slide 40

Slide 40 text

@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData(context = Dispatchers.IO) { emit(fetchOpeningHours(id)) }

Slide 41

Slide 41 text

@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel

Slide 42

Slide 42 text

@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel

Slide 43

Slide 43 text

@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel

Slide 44

Slide 44 text

@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } }

Slide 45

Slide 45 text

@jossiwolf Repository with Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData(context = Dispatchers.IO) { emit(fetchOpeningHours(id)) }

Slide 46

Slide 46 text

@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } }

Slide 47

Slide 47 text

@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = liveData(context = Dispatchers.IO) { val openingHours = repo.fetchOpeningHours(id) val filteredHours = openingHours.hours.filter { it #!= null } emit(filteredHours) } }

Slide 48

Slide 48 text

@jossiwolf # Converting to Flow

Slide 49

Slide 49 text

@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData { val liveData = MutableLiveData() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData

Slide 50

Slide 50 text

@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData { val liveData = MutableLiveData() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData

Slide 51

Slide 51 text

@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData { val liveData = MutableLiveData() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData

Slide 52

Slide 52 text

@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow

Slide 53

Slide 53 text

@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow

Slide 54

Slide 54 text

@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow

Slide 55

Slide 55 text

@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow

Slide 56

Slide 56 text

@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData = fetchSupermarketDetails(id).asLiveData()

Slide 57

Slide 57 text

@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO)

Slide 58

Slide 58 text

@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO)

Slide 59

Slide 59 text

@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId ): Flow = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO)

Slide 60

Slide 60 text

@jossiwolf - developer.android.com/topic/libraries/architecture/livedata - proandroiddev.com/dont-use-livedata-in- repositories-f3bebe502ed3 - medium.com/@elizarov/execution-context-of-kotlin-flows-b8c151c9309b Resources

Slide 61

Slide 61 text

@jossiwolf

Slide 62

Slide 62 text

Jossi Wolf @jossiwolf From LiveData to Coroutines Flow