Difference of setValue() & postValue() in MutableLiveData

livedata post value not working
difference between livedata and mutablelivedata
livedata observer not called
android mutablelivedata example
mediator live data
android-livedata observer called multiple times
mutablelivedata<boolean
mutablelivedata constructor

There are two ways that make change value of MutableLiveData. But what is difference between setValue() & postValue() in MutableLiveData.

I could not find documentation for same.

Here is class MutableLiveData of Android.

package android.arch.lifecycle;

/**
 * {@link LiveData} which publicly exposes {@link #setValue(T)} and {@link #postValue(T)} method.
 *
 * @param <T> The type of data hold by this instance
 */
@SuppressWarnings("WeakerAccess")
public class MutableLiveData<T> extends LiveData<T> {
    @Override
    public void postValue(T value) {
        super.postValue(value);
    }

    @Override
    public void setValue(T value) {
        super.setValue(value);
    }
}

Based on the documentation:

setValue():

Sets the value. If there are active observers, the value will be dispatched to them. This method must be called from the main thread.

postValue():

Posts a task to a main thread to set the given value. If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

To summarize, the key difference would be:

setValue() method must be called from the main thread. But if you need set a value from a background thread, postValue() should be used.

LiveData setValue vs postValue in Android, The clear difference is that setvalue cannot exclude some controls while the patchvalue is able to do just that. So let's assume we have a formgroup with 2 controls:  Updating Form controls from a model is very easy and flexible in Reactive Form API of Angular v2. So, setValue () and patchValue () are the methods by which we can update the Reactive forms controls from our model. Let us see the difference between these two in the following example.

All above answers are correct. But one more important difference. If you call postValue() on field that has no observers and after that you call getValue(), you don't receive the value that you set in postValue(). So be careful if you work in background threads without observers.

Setvalue vs Patchvalue Angular - Sami C., So, setValue() and patchValue() are the methods by which we can update the Reactive forms controls from our model. Let us see the difference  While the setValue() method is used to set the changed value i.e. the change value will be dispatched to the active observer of the application. This setValue() method must be called from the main thread.

setValue()

Sets the value. If there are active observers, the value will be dispatched to them.

This method must be called from the main thread.

postValue

If you need set a value from a background thread, you can use postValue(Object)

Posts a task to a main thread to set the given value.

If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

Difference Between setValue() And patchValue() In Angular 2, There are two ways that make change value of MutableLiveData . But what is difference between setValue() & postValue() in MutableLiveData . The setValue() method in org.javatuples is used to set the value in the LabelValueClassObject or KeyValueClassObject. This method can be used with only LabelValue class or KeyValue class of javatuples library. It returns another ClassObject with the value as the element passed as the parameter, and the key or label from the previous ClassObject.

setValue() is called directly from caller thread, and synchronously notifies observers. Also it can be called only from MainThread. postValue() uses inside something like this new Handler(Looper.mainLooper()).post(() -> setValue()), so it runs setValue via Handler in MainThread. Also it can be called from any thread.

Difference of setValue() & postValue() in MutableLiveData , Developer Website, then it says that: LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. Setvalue and Patchvalue are methods from the Angular Formgroup. They both set the value of a control in a formgroup. The clear difference is that setvalue cannot exclude some controls while the patchvalue is able to do just that.

setValue() method must be called from the main thread. If you need set a value from a background thread, you can use postValue().

More here .

Necessity of ViewModel and Difference Between MutableLiveData , holder that contains primitive/collection types. It's used for observing changes in the view and updating the view when it is ACTIVE. check the value immediately after the SetValue node. It was not a legal value. I tried another means. With this method, you can retrieve the legal value. What are these differences? SetValue is fail. SetValue is success.

Android LiveData, framework manages the lifecycles of UI controllers, such as activities and fragments. SetValue(DependencyProperty, Object) Sets the local value of a dependency property, specified by its dependency property identifier. SetValue(DependencyPropertyKey, Object) Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

ViewModel Overview, LiveData which publicly exposes setValue(T) and postValue(T) method. Summary. Public constructors. MutableLiveData()  The SetValue(Object, Object) overload sets the value of a non-indexed property. To determine whether a property is indexed, call the GetIndexParameters method. If the resulting array has 0 (zero) elements, the property is not indexed.

MutableLiveData, Hi Can someone explain to me what is the difference between setText() and setValue() Because it seems to me so far both are doing the same thing. Thanks​  We even use normal CLR property to wrap around a dependency property and use GetValue and SetValue to get and set values passed within it. This is almost the same as CLR property system. So, what are the advantages of the new property system. Let's see the difference between Dependency property and CLR property.

Comments
  • "only the last value would be dispatched". I can't be sure about it by reading the code. So it seems like once the first thread about to hit the inner synchronised block inside postValue(), the next CPU window can be potentially given to the thread 2 which is posting another value. Thread 2 can then complete the synchronised block and the scheduler gives the first thread a window to run itself. Now, it overrites what thread 2 has already written. Is this possible?
  • Wish I could triple-upvote! Based on this, it appears it's best to use setValue() if possible, and cautiously use 'postValue()', only when necessary. Thanks
  • No, here isn't any "best" way. If you work with your LiveData from background thread you should to use postValue. Also in latest version of lifecycle components it fixed... probably.
  • "Also in latest version of lifecycle components it fixed... probably." Do you have any more information on this? Thanks
  • I made some tests and seems that with last version of lib everything works as it should.
  • Could you show me the above concretely code? If In ViewModel, I Implemented like noObserveLiveData.postValue("sample"), In Activity, when I used getValue like viewModel.noObserveLiveData.getValue Do you mean Is it not the value that I set in postValue() ("sample")?
  • Nice, I like this. In Kotlin I created an extension that encapsulates the smart update so the numerous value updates throughout my app are a single, consistent call.