Open In App

LiveData setValue vs postValue in Android

Improve
Improve
Like Article
Like
Save
Share
Report

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.

What exactly is LiveData?

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:

  1. There are no memory leaks.
  2. Views always receive the most recent data.
  3. There was no crash as a result of the halting of activities.

What is the difference between setValue() and postValue()?

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.

Difference Table

setValue()

postValue()

The setValue is a class that holds observable data. The postValue() requires certain observable values
Live data is lifecycle-aware.  postValue() is not lifecycle aware.
The postValue method’s responsibility is to post or add a task to the application’s main thread whenever the value changes The postValue method’s responsibility is to post or add a task to the application’s main thread whenever the value changes
The value will be updated whenever the main thread runs.  The value will be updated whenever the main thread runs later 
You cannot use setValue if you are working in a background thread You can use postValue if you are working in a background thread
The value is changed immediately The value is changed after an interval
In the case of setValue() the said value is called twice, and the value is updated twice, and the observers are notified about the updated data twice. In the case of postValue(), the value will be updated twice, and the number of times the observers will receive the notification is determined by the main thread’s execution. 

Last Updated : 28 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads