This library provides RxJava-style operators for your LiveData objects.
filter,filterNot: emit only elements that match, or don't match, a predicatefilterNotNull: dropsnullelementsfilterIsInstance: emits only elements of a given typedistinct: emits each element only oncedistinctUntilChanged: drops elements equal to the previous one, by key or comparerfirst,take,takeUntil,takeWhile: emit the first elements, then stopelementAt: emits only the element at a given indexskip,skipUntil,skipWhile,skipLast: skip the first or the last elementssample: emits the latest element whenever anotherLiveDataemits
mapNotNull: transforms elements and dropsnullresultscast: casts elements to a given typescan: emits running accumulations of the elementsbuffer: emits elements in lists of a given sizezipWithNext: pairs each element with the one before itswitchLatest: follows the latestLiveDataemitted by aLiveDataofLiveDatas
merge,mergeArray,mergeWith,+: emit the elements of all sourcescombineLatest,combineLatestWith: combine the latest elements of all sourceswithLatestFrom: combines each element with the latest element of anotherLiveDataamb,ambWith: follow whichever source emits firststartWith: emits given elements before the source's own
doOnValue,doOnActive,doOnInactive: run code for each element, or when the result becomes active or inactive
map, switchMap and distinctUntilChanged() without arguments come from AndroidX Lifecycle (androidx.lifecycle).
val source: LiveData<Int> = // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
source.filter { it % 2 == 1 }.observe(this) {
println(it) // 1, 3, 5, 7, 9
}val source: LiveData<Int> = // 1, 2, 1, 3, 2, 4, 1, 3, 2, 5
source.distinct().observe(this) {
println(it) // 1, 2, 3, 4, 5
}val source1: LiveData<Int> = // 1, 2, 3
val source2: LiveData<Int> = // 10, 20, 30
listOf(source1, source2).merge().observe(this) {
println(it) // 10, 1, 20, 2, 30, 3
}val input: LiveData<String> = // "a", "ab", "abc"
val submits: LiveData<Unit> = // Unit, Unit
submits.withLatestFrom(input) { _, text -> text }.observe(this) {
println(it) // "ab", "abc"
}- Like
LiveDataitself, all operators must be called on the main thread. LiveDatakeeps only its latest value, so operators see only the elements they receive while the returnedLiveDatais active.
Available on Maven Central.
dependencies {
implementation("de.musichin.reactivelivedata:reactivelivedata:2.0.0")
}| ReactiveLiveData | minSdk | Kotlin | AndroidX Lifecycle |
|---|---|---|---|
| 2.x | 23 | ≥ 2.1 | ≥ 2.11.0 |
| 1.2.x | 14 | ≥ 1.8 | ≥ 2.6.2 |
map,switchMapanddistinctUntilChanged()were removed, because AndroidX Lifecycle provides the same functions. Import them fromandroidx.lifecycleinstead; the calls stay the same.combineLatestandmergereturnLiveData<T>instead ofLiveData<out T>, so casts at call sites are no longer needed.liveData(),liveData(value)andliveData { }were renamed toemptyLiveData(),liveDataOf(value)andlazyLiveData { }, becauseliveData { }clashed with the coroutine builder of AndroidX Lifecycle.
See the 2.0.0 release notes for all changes.
Contributions are welcome: open a pull request that describes your changes.
Open a GitHub issue for bugs or feature requests.
MIT, see LICENSE.