Class diagrams associated with LiveData



There are fewer classes involved in LiveData and they are not that complex. To read the source code for LiveData, LifeCycle will be read first because there is something that will LifeCycle, Value will only be distributed if LifeCycle is STARTED or RESUMED. Why? Live data, Live data, Live, Live, Live. However, there are exceptions, observerForever (observer: observer <? Super T>) this will add an Observer that LifeCycle will receive data from setValue no matter what state LifeCycle is in. Why will be explained later.

Observe(LifecycleOwner,Observer Observer) method of LiveData

LiveData is an abstract class, but looking at the source code there are no abstract methods, but there are several empty methods, onActive() and onInactive(), what these two methods do, there is a question. So let’s start with the Observe method

1. Determine whether the current method is called by the main thread, if not the main thread will throw an exception

So it follows that Observe cannot be called in child threads. The assertion here, which involves the source code for ArchTaskExecutor, I’m going to skip this part of the introduction, and if you’re interested you can look at the default Executor, DefaultTaskExecutor, With a pool of two IO threads and a main thread Handler, this TaskExecutor can give some insight into writing asynchronous task management.

LifeCycle will be LifeCycle if it is Destory.

3. Create a LifecycleBoundObserver object

LifecycleBoundObserver inherits the ObserverWrapper class and implements the GenericLifecycleObserver interface, so its job is to listen for life cycle transitions and record the current state. And distribute value only if the state is greater than or equal to STARTED. This code here is shouldBeActive. The Observer is removed when LifecycleOwner’s life cycle becomes DESTORY. The constructor of this object involves two objects: LifecycleOwner and Observer. The first object is designed to capture the state of the current life cycle at any time. The second object is asking the distribution value,

The same ObserverWrapper cannot be added to different lifecycle applications as described in the previous lifecycle source.

5. Attach LifecycleBoundObserver to LifeCycleOwner and start listening for lifecycle switches.

SetValue (T value), postValue(T value) and dispatchingValue(ObserverWrapper initiator)

SetValue (T value) assigns a new value to mData at the same time, mVersion+1, then dispatchingValue(null), dispatchingValue if the parameter is not null, If the value is null, it will iterate through the Observer saved in the map collection and distribute the value. PostValue () is usually called in the child thread.

considerNotity(observer)

This method is called in the dispatchingValue method to verify that the current status of the Observer is greater than or equal to STARTED and will be distributed if so.

OnActive () and onInactive ()

Where are these two methods called? The answer is that the activeStateChange(Boolean Active) method of the ObserverWraper abstract class is called, and the activeStateChange method is called during the lifecycle callback, And when setValue is invoked and the current lifecycle state is not equal to terminated, OnActive () is called when the life cycle state is greater than STARTED, and value() is distributed and onInactive() is called if the life cycle state is less than STARTED

To summarize, OnActive() will be called when LifeCycle is greater than STARTE and OnInactive() will be called when LifeCycle is less than STARTED.

conclusion

LiveData basically monitors life cycle states and distributes related values when life cycle states are greater than or equal to STARTED. In DESTORY, it actively removes the Observer. Of course, there are also observers that can distribute values in any state. The AlwaysActiveObserver method shoudBeActive always returns true, regardless of the lifecycle and state.

The last

Still that sentence, write bad, if where write wrong words, hope to point out me this lost little bookboy.