EmptyLiveData

An empty LiveData implementation for situations where we need a LiveData instance which will always have a null value.

When using this, observers will receive a callback for the null value of this instance. Because of this, EmptyLiveData can only be used for nullable types T. For non-nullable types T where no callback should be emitted, UnsetLiveData can be used instead.

Constructors

Link copied to clipboard

Inherited functions

Link copied to clipboard
open fun getValue(): T?
Link copied to clipboard
Link copied to clipboard
open fun hasObservers(): Boolean
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.any(predicate: (E) -> Boolean): Boolean
Link copied to clipboard
fun <T> LiveData<T>.check(predicate: (T) -> Boolean): Boolean
Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.contains(element: E): Boolean

operator fun <K> LiveData<out Map<K, *>>.contains(key: K): Boolean

Returns true if the LiveData's value contains the specified key.

Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.containsAll(elements: Collection<E>): Boolean
Link copied to clipboard
fun <K> LiveData<out Map<K, *>>.containsKey(key: K): Boolean

Returns true if the LiveData's value contains the specified key.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.containsValue(value: V): Boolean

Returns true if the LiveData's value maps one or more keys to the specified value.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.find(predicate: (Map.Entry<K, V>) -> Boolean): Map.Entry<K, V>?

Finds the first entry from a LiveData's Map's value that matches the given predicate. Returns null if no entry is found or if the LiveData has no value.

Link copied to clipboard
fun <T, S> LiveData<out Collection<S>>.flatMap(mapFunction: (S) -> LiveData<out Collection<T>>): LiveData<Collection<T>>

Transforms a LiveData containing a list of type T, which contains all the child elements from each element in this LiveData list by flattening it.

Link copied to clipboard
fun <T, S> LiveData<out Collection<S>>.flatMapBySource(mapFunction: (S) -> LiveData<out Collection<T>>): LiveData<Collection<Pair<S, T>>>

Transforms a LiveData containing a list of type T, which contains all the child elements from each element in this LiveData list together with its source, by flattening it.

Link copied to clipboard
fun <E> LiveData<out Iterable<E>?>.forEach(operation: (E) -> Unit)

Performs operation on each entry in a LiveData's collection. Does nothing if the LiveData has no value.

fun <K, V> LiveData<out Map<K, V>>.forEach(operation: (Map.Entry<K, V>) -> Unit)

Performs operation on each entry in a LiveData's Map. Does nothing if the LiveData has no value.

Link copied to clipboard
operator fun <K, V> LiveData<out Map<K, V>>.get(key: K): V?

Returns the value corresponding to the given key in the LiveData's value, or null if such a key is not present in the map or the LiveData has no value.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.getOrDefault(key: K, defaultValue: V): V

Returns the value corresponding to the given key in the LiveData's value, or defaultValue if such a key is not present in the map or the LiveData has no value.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.getOrElse(key: K, defaultValue: () -> V): V

Returns the value for the given key in the LiveData's value, or the result of the defaultValue function if there was no entry for the given key or the LiveData has no value.

Link copied to clipboard
fun <T, B : Boolean?> LiveData<B>.ifTrue(block: () -> LiveData<T>): LiveData<T?>

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.

Link copied to clipboard
fun <T : Any, B : Boolean?> LiveData<B>.ifTrueOrDefault(default: T, block: () -> LiveData<T>): LiveData<T>

Executes and switchMaps to block if this has a true value. If this is false or null, it returns LiveData with value default. This can be used instead of ifTrue when the default value should be something other than null. Note that this requires a non-null value for T in order for Kotlin's compiler to properly spot missing null-checks.

Link copied to clipboard

Maps the value of the provided key, if that key exists in this map. Otherwise, no value is set. This is useful in cases where the map may not contain the requested entry but the resulting LiveData is not nullable as the client does not care about a null value. This removes the need for the client to have null checks in its code wherever the resulting LiveData is used.

Link copied to clipboard
fun <T : Any, S : Any> LiveData<out Collection<S>>.merge(mapFunction: (S) -> LiveData<out T?>): LiveData<out Collection<T>>

Transforms LiveData containing a list that holds other LiveData instances into LiveData that contains the values from those LiveData instances. The returned LiveData will be updated when the source LiveData changes as well as when the internal LiveData instances change.

Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.none(predicate: (E) -> Boolean): Boolean
Link copied to clipboard
operator fun <T : Boolean?> LiveData<T>.not(): LiveData<T>

Negates the value of a boolean LiveData.

Link copied to clipboard

Same as LiveData.getValue, except this variant throws a IllegalStateException when the LiveData instance does not have an active observer.

Link copied to clipboard
inline fun <T : Any> LiveData<T>.observeNonNull(owner: LifecycleOwner, crossinline onChanged: (T) -> Unit): Observer<T>

Adds the given onChanged lambda as an observer within the lifespan of the given owner and returns a reference to observer. The events are dispatched on the main thread.

Link copied to clipboard
inline fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, crossinline onChanged: (T) -> Unit): Observer<T>

Observes this LiveData for one change only and returns the one-time observer. The returned observer can be used to stop observing the LiveData when a callback is no longer desired. It is safe to remove the observer while it is executing or has been executed already.

Link copied to clipboard
inline fun <T> LiveData<T>.observeWhile(lifecycleOwner: LifecycleOwner, crossinline onChanged: (T) -> Boolean): Observer<T>

Observes this LiveData as long as onChanged returns true and returns the observer. The returned observer can be used to stop observing the LiveData when a callback is no longer desired. It is safe to remove the observer while it is executing or after onChanged returns true.

Link copied to clipboard
fun <T : R?, R : Any> LiveData<T>.requireValue(): R

Gets the value stored in the LiveData object that should not be null.

Link copied to clipboard

Same as LiveData.getValue with the exception that the returned value is up to date even when the LiveData instance does not have an observer.

Link copied to clipboard
fun <T> LiveData<T>.withSetter(setter: (T) -> Unit): MutableLiveData<T>

Creates a MutableLiveData from LiveData by specifying the setter to use to the change the value.