我们可以在不失去任何价值的情况下使用 LiveData 吗?

lol*_*f64 2 android android-livedata

I would like to use a LiveData for handling kind of notifications, as it is already lifecycle aware, between a custom view and its wrapping fragment. But it seems that a LiveData may loose values : it will only update to its most recent state and also won't fire values during inactive state of its observers.

I've looked at the SingleLiveEvent purpose from Google code samples, but that solution does not seems to be battle tested yet, and the ticket is still open with recent tries to improve the solution.

So I am looking for a simple way to get notified about events, and at the same time not being worried about Lifecycles (that was why I went for LiveData as a first solution), and that could handle multiple observers.

Is there an existing solution for that ? If I try to implement it, it is sure that I will land into at least an anti-pattern.

One easy way (perhaps too easy) is to use callbacks : but the problem is that I need this feature for several callbacks in my component, leading me in a poor architecture. And also, I want a subscribe system, meaning that there could be more than one observer.

One other way, could be to use RxJava and tranform it into a LiveData, with LiveDataReactiveStreams.fromPublisher() : but now the question is whether I will get all values or only the last one. That's the closest solution I could deal with.

As an interesting alternative there could be AutoDispose or RxLifecycle. And an interesting resource I've found : Blog post on LiveData

你有什么想法、建议?

另外,请注意,我需要从包装到 Fragment (ChessBoard) 的组件到另一个 Fragment (ChessHistory) 的通信。因此,它们都具有生命周期意识。

Jul*_*ñas 5

这并不理想,但这对我有用:

/**
* This LiveData will deliver values even when they are 
* posted very quickly one after another.
*/
class ValueKeeperLiveData<T> : MutableLiveData<T>() {

    private val queuedValues: Queue<T> = LinkedList<T>()

    @Synchronized
    override fun postValue(value: T) {
        // We queue the value to ensure it is delivered 
        // even if several ones are posted right after.
        // Then we call the base, which will eventually
        // call setValue().
        queuedValues.offer(value)
        super.postValue(value)
    }

    @MainThread
    @Synchronized
    override fun setValue(value: T) {
        // We first try to remove the value from the queue just
        // in case this line was reached from postValue(),
        // otherwise we will have it duplicated in the queue.
        queuedValues.remove(value)

        // We queue the new value and finally deliver the
        // entire queue of values to the observers.
        queuedValues.offer(value)
        while (!queuedValues.isEmpty())
            super.setValue(queuedValues.poll())
    }
}
Run Code Online (Sandbox Code Playgroud)

此解决方案的主要问题是,如果观察者在通过 传递值时处于非活动状态super.setValue(),那么无论如何这些值都会丢失。然而,当几个新的发布非常快时,它解决了丢失值的问题——在我看来,这通常比丢失值更大的问题,因为你的观察者是不活跃的。毕竟,您始终可以myLiveData.observeForever()从非生命周期感知对象中执行以接收所有通知。

不确定这对你来说是否足够,但我希望它可以帮助你或给你一些关于如何实施你自己的方法的想法。