ifTrue
fun <T, B : Boolean?> LiveData<B>.ifTrue(block: () -> LiveData<T>): LiveData<T?>
Content copied to clipboard
Executes and switchMaps to block if this
has a true
value. If this
is false
or null
, it returns LiveData with value null
. If this should not return null
, ifTrueOrDefault can be used instead.
This can be used to replace:
myLiveData.switchMap {
if (it == true) {
myNewLiveData
} else {
EmptyLiveData()
}
}.distinctUntilChanged()
Content copied to clipboard
with a more concise syntax:
myLiveData.ifTrue { myNewLiveData }
Content copied to clipboard