LiveData

expect abstract class LiveData<T>

LiveData is an abstract class describing a data holder class that is observed within a lifetime.

The data object will notify its Observer in the following scenarios:

  1. When the observation starts: LiveData.observe.

  2. When the Observer moves from not observing to observing.

  3. When the value changes: LiveData.getValue.

The Observer will be automatically removed upon termination of the LifecycleOwner.

LiveData instances are not thread-safe and assume that all interaction occurs on a single thread.

Inheritors

actual typealias LiveData<T> = NLiveData<T>

Constructors

Link copied to clipboard
expect constructor()

Functions

Link copied to clipboard
fun <T : Any> LiveData<OnceEvent<T>>.consume(owner: LifecycleOwner, block: (T) -> Unit): Observer<OnceEvent<T>>

Consume the event as dispatched from the data object.

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

Get the current value of the data.

Link copied to clipboard
expect open fun observe(owner: LifecycleOwner, observer: Observer<in T>)

Observe the data object.

Link copied to clipboard
expect open fun observeForever(observer: Observer<in T>)

Observe the data object forever.

Link copied to clipboard
fun <T> LiveData<T>.observeForeverInMain(observer: Observer<in T>)

在主线程中进行订阅通知

Link copied to clipboard
expect open fun removeObserver(observer: Observer<in T>)

Remove the given observer, preventing further changes from being dispatched.