Share
Embed
From LiveData to Coroutines & Flow - DevFest ZA
Slides from my talk @DevFest ZA 2020!
Jossi Wolf
October 16, 2020
Transcript
75 slides
-
@jossiwolf ”LiveData is an observable data holder class” - Android
Developers Documentation -
@jossiwolf class HomeViewModel { val latestData: LiveData<String> = MutableLiveData() }
LiveData - an example -
@jossiwolf class HomeViewModel { val latestData: LiveData<String> = MutableLiveData() fun
refresh() { val data = fetchData() latestData.post(data) } } LiveData - an example -
@jossiwolf class HomeFragment: Fragment() { fun setupUI() { latestData.observe(viewLifecycleOwner) {
data #-> ##... } } } LiveData - an example -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf class HelpViewModel( private val supermarketRepo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = supermarketRepo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> 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 -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> 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 -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> 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 -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> 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 -
@jossiwolf <X, Y> LiveData<Y> map( LiveData<X> source, Function<X, Y> mapFunction
) { MediatorLiveData<Y> 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 -
@jossiwolf ”The events are dispatched on the main thread.” -
Android Developers Documentation -
@jossiwolf fun fetchOpeningHours(id: SupermarketId): LiveData<OpenHoursInformation> { val liveData = MutableLiveData<OpenHoursInformation>()
val openingHours = poiService.downloadOpeningHours(poiType = SUPERMARKET, id).enqueue( object: Callback<OpenHoursInformation>() { override fun onResponse(call: Call<OpenHoursInformation>, response: Response<OpenHoursInformation>) { liveData.postValue(response.body()) } ##... } ) liveData.postValue(openingHours) return liveData } Repository with LiveData + Callbacks -
@jossiwolf suspend fun fetchOpeningHours(id: SupermarketId): LiveData<OpenHoursInformation> { val liveData =
MutableLiveData<OpenHoursInformation>() val openingHours = poiService.downloadOpeningHours(poiType = SUPERMARKET, id) liveData.postValue(openingHours) return liveData } Repository with LiveData + Coroutines -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@jossiwolf With Migration Helper suspend fun fetchOpeningHours(id: SupermarketId) = poiService.downloadOpeningHours(poiType
= SUPERMARKET, id) @Deprecated fun fetchOpeningHoursAsLiveData(id: SupermarketId) = liveData { emit(fetchOpeningHours(id)) } -
@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)) } -
@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHours(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf class HelpViewModel( private val repo: SupermarketRepository ): ViewModel() {
fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } Our ViewModel -
@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ):
ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@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)) } -
@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ):
ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) = repo.fetchOpeningHoursAsLiveData(id).map { openingHours #-> openingHours.hours.filter { it #!= null } } } -
@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) } } -
@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData<SupermarketDetails> { val
liveData = MutableLiveData<OpenHoursInformation>() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData -
@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData<SupermarketDetails> { val
liveData = MutableLiveData<OpenHoursInformation>() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData -
@jossiwolf suspend fun fetchSupermarketDetails( id: SupermarketId ): LiveData<SupermarketDetails> { val
liveData = MutableLiveData<OpenHoursInformation>() val cachedDetails = detailsCache[id] if (cachedDetails #!= null) liveData.postValue(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) liveData.postValue(freshDetails) return liveData } With LiveData -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf fun fetchSupermarketDetails( id: SupermarketId ): Flow<SupermarketDetails> = flow {
val cachedDetails = detailsCache[id] if (cachedDetails #!= null) emit(cachedDetails) val freshDetails = poiService.downloadDetails(SUPERMARKET, id) emit(freshDetails) } With Flow -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData() -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO) -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO) -
@jossiwolf With Flow + Migration Helper fun fetchSupermarketDetails( id: SupermarketId
): Flow<SupermarketDetails> = flow { … } @Deprecated("Please use Flows directly.") fun fetchSupermarketDetailsAsLiveData( id: SupermarketId ): LiveData<SupermarketDetails> = fetchSupermarketDetails(id).asLiveData(Dispatchers.IO) -
@jossiwolf “A Flow that represents a read-only state with a
single updatable data value that emits updates to the value to its collectors.” - StateFlow Documentation # StateFlow -
@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ):
ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) : LiveData<OpeningHoursState> } -
@jossiwolf Our ViewModel class HelpViewModel( private val repo: SupermarketRepository ):
ViewModel() { fun fetchSupermarketOpeningHours(id: SupermarketId) : Unit } -
@jossiwolf Our ViewModel class HelpViewModel(…): ViewModel() { val _openingHoursState =
MutableStateFlow(OpeningHoursState.Empty) val openingHoursState: StateFlow<OpeningHoursState> = _openingHoursState fun fetchSupermarketOpeningHours(id: SupermarketId) { viewModelScope.launch(Dispatchers.IO) { val openingHours = repo.fetchOpeningHours() _openingHoursState.value = openingHours } } } -
@jossiwolf Our ViewModel class HelpViewModel(…): ViewModel() { val _openingHoursState =
MutableStateFlow(OpeningHoursState.Empty) val openingHoursState: StateFlow<OpeningHoursState> = _openingHoursState fun fetchSupermarketOpeningHours(id: SupermarketId) { viewModelScope.launch(Dispatchers.IO) { val openingHours = repo.fetchOpeningHours() _openingHoursState.value = openingHours } } } -
@jossiwolf Our ViewModel class HelpViewModel(…): ViewModel() { private val _openingHoursState
= MutableStateFlow(OpeningHoursState.Empty) val openingHoursState: StateFlow<OpeningHoursState> = _openingHoursState fun fetchSupermarketOpeningHours(id: SupermarketId) { viewModelScope.launch(Dispatchers.IO) { val openingHours = repo.fetchOpeningHours() _openingHoursState.value = openingHours } } } -
@jossiwolf All methods of data flow are thread- safe and
can be safely invoked from concurrent coroutines without external synchronization. - StateFlow Documentation -
@jossiwolf -Single API instead of two APIs -More control over
execution context Why StateFlow? -
@jossiwolf -Single API instead of two APIs -More control over
execution context -Integrates nicely with Jetpack Compose Why StateFlow? -
@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 - https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/
kotlinx.coroutines.flow/-state-flow/ Resources