LiveData transformations
The basics…Transforming LiveData is pretty easy to do and there is an awesome helper class named Transformations for just this purpose. This class provides three static methods:
mapTransforms the value of
switchMapTransforms the value of a
distinctUntilChangedFilters
This is a really neat feature and I tend to use it quite often in my code. For my use-cases, it’s mostly related to
livedata-ktx extensions for TransformationsAll of the Transformations class functions above are also available as extension functions on LiveData using the dependency: androidx.lifecycle:lifecycle-livedata-ktx:<version> With this you can, for instance, rewrite the example above as:
Behind the scenes of the Transformations classWe’ve just covered 3 simple transformations that you can actually write your self. All of them are written using the To give you an example of when you should be creating your own
By only mapping
With this solution, I’m getting game updates whenever either
MediatorLiveData
In this example, I’m observing two LiveData sources (a and b). I’m calling the
OPTION 1What are the sources you want to monitor for changes before emitting anything from this
OPTION 2Call the inner
OPTION 3Since
OPTION 4Since the
ConclusionsUse Use If you find yourself getting the current value of another
BonusNow that you know how https://github.com/ptornhult/livedata-utils Thanks to Erik Hellman.
Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
By Carlos Mota It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update.
By
ProAndroidDev -
droidcon News
Tech Showcases, Developer Resources & Partners
EmployerBrandingHeader
jobs.droidcon.com
![]() Latest Android Jobs
Kotlin Weekly
![]() Your weekly dose of Kotlin
ProAndroidDev
![]() Android Tech Blogs, Case Studies and Step-by-Step Coding
Zalando
![]() Meet one of Berlin's top employers
Academy for App Success
![]() Google Play resources tailored for the global droidcon community |
Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.