KOTLIN: setState vs postState
This is something that is a very common confusion point when setting values of mutable live data objects in KOTLIN so I thought I would create a small article for people to understand it better.
postState()
public void postValue(T value) {
super.postValue(value);
}
The official documentation states that
Posts a task to the 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.
Simply put in simpler terms, it means that postState() is called on a separate thread( other than the main thread ). What that means is that postState() will not set the value synchronously so if you call a getValue() right after calling a postValue(), it may not necessarily give you the newly set value since the thread might not have executed yet.
Also, If you called this method multiple times before the main thread executed a posted task, only the last value would be dispatched.
setState()
public void postValue(T value) {
super.postValue(value);
}
The official documentation states that
Sets the value. If there are active observers, the value will be dispatched to them…