Created
June 29, 2020 08:05
-
-
Save iniyanmurugavel/8e90b654391d30e463ca0a9915e950fb to your computer and use it in GitHub Desktop.
LiveData Useful for Understanding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://blog.mindorks.com/understanding-livedata-in-android | |
| LiveData considers an observer, which is represented by the Observer class, to be in an active state if its lifecycle is in the STARTED or RESUMED state. LiveData only notifies active observers about updates. Inactive observers registered to watch LiveData objects aren't notified about changes. | |
| Features | |
| Ensures your UI matches your data state | |
| No memory leaks | |
| No crashes due to stopped activities | |
| No more manual lifecycle handling | |
| Always up to date data | |
| Proper configuration changes | |
| If an activity or fragment is recreated due to a configuration change, like device rotation, it immediately receives the latest available data. | |
| LiveData allows UI controller observers to subscribe to updates. When the data held by the LiveData object changes, the UI automatically updates in response. | |
| LiveData is a wrapper that can be used with any data, including objects that implement Collections, such as List. A LiveData object is usually stored within a ViewModel object and is accessed via a getter method, | |
| // Create a LiveData with a String | |
| val currentName: MutableLiveData<String> by lazy { | |
| MutableLiveData<String>() | |
| } | |
| private val model: NameViewModel by viewModels() | |
| LiveData has no publicly available methods to update the stored data. | |
| LiveData has no publicly available methods to update the stored data. The MutableLiveData class exposes the setValue(T) and postValue(T) methods publicly and you must use these if you need to edit the value stored in a LiveData object. Usually MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable LiveData objects to the observers. | |
| Merge multiple LiveData sources | |
| MediatorLiveData is a subclass of LiveData that allows you to merge multiple LiveData sources. Observers of MediatorLiveData objects are then triggered whenever any of the original LiveData source objects change. | |
| Your activity only needs to observe the MediatorLiveData object to receive updates from both sources | |
| You may want to make changes to the value stored in a LiveData object before dispatching it to the observers, or you may need to return a different LiveData instance based on the value of another one. The Lifecycle package provides the Transformations class which includes helper methods that support these scenarios. | |
| Transformations.map() | |
| val userLiveData: LiveData<User> = UserLiveData() | |
| val userName: LiveData<String> = Transformations.map(userLiveData) { | |
| user -> "${user.name} ${user.lastName}" | |
| } | |
| Transformations.switchMap() | |
| Similar to map(), applies a function to the value stored in the LiveData object and unwraps and dispatches the result downstream. The function passed to switchMap() must return a LiveData object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment