CachedSliceLiveData
class CachedSliceLiveData : LiveData<Slice!>
Implementation of LiveData
that provides controls over how cached vs live slices work.
Summary
Public methods
|
open Unit |
Moves this CachedSliceLiveData into a "live" state, causing the providing app to start up and provide an up to date version of the slice.
|
open Unit |
Generally the InputStream are parsed asynchronously once the LiveData goes into the active state.
|
Inherited functions
|
From class LiveData
T? |
getValue()
Returns the current value. Note that calling this method on a background thread does not guarantee that the latest value set will be received.
|
Boolean |
hasActiveObservers()
Returns true if this LiveData has active observers.
|
Boolean |
hasObservers()
Returns true if this LiveData has observers.
|
Unit |
observe(@NonNull owner: LifecycleOwner, @NonNull observer: Observer<in T>)
Adds the given observer to the observers list within the lifespan of the given owner. The events are dispatched on the main thread. If LiveData already has data set, it will be delivered to the observer.
The observer will only receive events if the owner is in Lifecycle.State#STARTED or Lifecycle.State#RESUMED state (active).
If the owner moves to the Lifecycle.State#DESTROYED state, the observer will automatically be removed.
When data changes while the owner is not active, it will not receive any updates. If it becomes active again, it will receive the last available data automatically.
LiveData keeps a strong reference to the observer and the owner as long as the given LifecycleOwner is not destroyed. When it is destroyed, LiveData removes references to the observer & the owner.
If the given owner is already in Lifecycle.State#DESTROYED state, LiveData ignores the call.
If the given owner, observer tuple is already in the list, the call is ignored. If the observer is already in the list with another owner, LiveData throws an IllegalArgumentException .
|
Unit |
observeForever(@NonNull observer: Observer<in T>)
Adds the given observer to the observers list. This call is similar to LiveData#observe(LifecycleOwner, Observer) with a LifecycleOwner, which is always active. This means that the given observer will receive all events and will never be automatically removed. You should manually call removeObserver(Observer) to stop observing this LiveData. While LiveData has one of such observers, it will be considered as active.
If the observer was already added with an owner to this LiveData, LiveData throws an IllegalArgumentException .
|
Unit |
postValue(value: T)
Posts a task to a main thread to set the given value. So if you have a following code executed in the main thread:
liveData.postValue("a");
liveData.setValue("b");
The value "b" would be set at first and later the main thread would override it with the value "a".
If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.
|
Unit |
removeObserver(@NonNull observer: Observer<in T>)
Removes the given observer from the observers list.
|
Unit |
removeObservers(@NonNull owner: LifecycleOwner)
Removes all observers that are tied to the given LifecycleOwner .
|
Unit |
setValue(value: T)
Sets the value. If there are active observers, the value will be dispatched to them.
This method must be called from the main thread. If you need set a value from a background thread, you can use postValue(Object)
|
|
Public methods
goLive
open fun goLive(): Unit
Moves this CachedSliceLiveData into a "live" state, causing the providing app to start up and provide an up to date version of the slice. After calling this method the slice will always be pinned as long as this LiveData is in the active state.
If the slice has already received a click or goLive() has already been called, then this method will have no effect.
Once goLive() has been called, there is no way to reverse it, this LiveData will then behave the same way as one created using fromUri(Context, Uri)
.
parseStream
open fun parseStream(): Unit
Generally the InputStream are parsed asynchronously once the LiveData goes into the active state. When this is called, regardless of state, the slice will be read from the input stream and then the input stream's reference will be released when finished.
Calling parseStream() multiple times or after the stream has already been parsed asynchronously will have no effect.
Protected methods
onActive
protected open fun onActive(): Unit
onInactive
protected open fun onInactive(): Unit