Efficient learning model

What -->why--> How model is? --> Why? --> How to use? --> Realization principle --> Summary and share (Learn to apply)Copy the code

What is LiveData?

LiveData is an observable data store class. Unlike regular observable classes, LiveData has lifecycle awareness, meaning that it follows the lifecycle of other application components such as activities, fragments, or services. This awareness ensures that LiveData updates only application component observers that are in an active lifecycle state.Copy the code

Why use LiveData?

1. If an observer's life cycle is STARTED or RESUMED, LiveData considers the observer to be active. LiveData only notifies active observers of updates. Inactive observers registered to observe LiveData objects do not receive notification of changes. 2. You can register observers that pair with objects that implement the LifecycleOwner interface. With this relationship Lifecycle objects can be removed when the state of the corresponding Lifecycle object changes to DESTROYED. This is especially useful for activities and fragments because they can safely observe LiveData objects without fear of leakage (the system unsubscribe them immediately when the Activity and Fragment's life cycle is destroyed). Using LiveData has the following advantages: 1. Ensure that the interface conforms to the data state. LiveData follows the observer mode. LiveData notifies the Observer when the underlying data changes. Code can be consolidated to update the interface in these observers. This eliminates the need to update the interface every time your app data changes, because the observer will do it for you. 2. There will be no memory leak observer will bind to Lifecycle object and clean up after its associated Lifecycle has been destroyed. If the observer's life cycle is inactive (such as an Activity in the back stack), it will not receive any LiveData events. 4. You no longer need to manually process life cycle interface components. You just observe related data without stopping or resuming observation. LiveData automatically manages all of these operations because it can sense the associated lifecycle state changes as it observes. 5. Data is always up to date. If the life cycle becomes inactive, it receives the latest data when it becomes active again. For example, an Activity that was once in the background receives the latest data as soon as it returns to the foreground. 6. Appropriate configuration changes If an Activity or Fragment is recreated due to a configuration change (such as a device rotation), it immediately receives the latest available data. 7. Shared resources can use the singleton pattern to extend LiveData objects to encapsulate system services so that they can be shared across applications. The LiveData object is connected to the system service once, and then any observer that needs the corresponding resource only needs to observe the LiveData object.Copy the code

How to use LiveData?

1. Create an instance of LiveData to store some type of data. This is usually done in the ViewModel class. 2. Create an Observer object that defines the onChanged() method, which controls what happens when the data stored by the LiveData object changes. You can create an Observer in an interface controller, such as an Activity or Fragment. 3. Attach an Observer to a LiveData object using the observe() method. The observe() method takes the LifecycleOwner object. This causes the Observer to subscribe to the LiveData object to be notified of changes. In general, you can attach an Observer object to an interface controller, such as an Activity or Fragment. 4. When a value stored in a LiveData object is updated, it triggers all registered observers (as long as the attached LifecycleOwner is active). LiveData allows interface controller observers to subscribe to updates. When the data stored in the LiveData object is changed, the interface is automatically updated to respond. More details can be found on the official websiteCopy the code

Four. Implementation principle

Fifth, summarize and share