LiveData setValue vs postValue in Android

28 Dec, 2021
We deal with a lot of data in Android. One activity’s data is being used in another activity or fragment. For example, when some data changes, we can change the UI of an application. The main point out here is how to use the data efficiently and precisely use LiveData, which is part of Android Jetpack. The most commonly used LiveData methods are setvalue and postValue. By name, these two functions are quite confusing, as they appear to serve the same purpose. However, the reality is quite different.
If we look at the Android Developer Website’s definition of LiveData, we can see that:
Simply put, LiveData is a data holder that is used to monitor changes to a specific view and then update the corresponding change.
As a result, LiveData is used to simplify the task of implementing ViewModel. The best part about LiveData is that when your View is in the background, the data is not updated; however, when the View comes into the foreground, it only receives the updated data.
Some of the benefits of LiveData include:
There are no memory leaks.Views always receive the most recent data.There was no crash as a result of the halting of activities.
There are no memory leaks.
Views always receive the most recent data.
There was no crash as a result of the halting of activities.
So, if you’re familiar with LiveData, you’ve probably come across these two LiveData methods: setValue() and postValue(). So, one question that may have occurred to you is why there is a requirement for two methods setValue and postValue for the same functionality. Let’s look at an example to find out. Assume you want to use LiveData to update a value. As a result, you will have two options: either update the data on the main thread or update the data on the background thread. As a result, the use-case of setValue and postValue is limited to these two scenarios.
When changing the data on the Main thread, use the MutableLiveData class’s setValue method, and when changing the LiveData on the background thread, use the MutableLiveData class’s postValue method
GeekTip: So, in essence, it is requesting that the main thread set the new updated value and then notify the observers.
While the setValue method is used to set the changed value from the main thread, if there are any live observers attached to it, the updated value is also sent to those observers. The main thread must call the setValue method.
// setValue
someLiveDataField.setValue("aNewValue")
someLiveDataField.setValue("sameNewValueAgain")

// postValue
someLiveDataField.postValue("aNewValue")
someLiveDataField.postValue("sameNewValueAgain")
In the preceding example, the setValue function is called from the main thread, while the postValue function is called from a background thread.
For example, if postValue is called four times before the main thread is executed, the observer will receive the notification only once, and that too with the most recently updated data, because the notification is scheduled to be executed on the main thread. So, if you call the postValue method multiple times before the main thread executes, the value that was passed last, i.e. the latest value will be used, and all the previous ones will be discarded 
Another thing to keep in mind about postValue is that if the field on which the postValue is called does not have any observers and you then call getValue, you will not receive the value that you set in the postValue because there is no observer here.
setValue()
postValue()
sagartomar9927
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.