val test = MutableLiveData("")
fun bind1() {
test.addObserver {
println("first $it")
if (it == "A") {
test.value = "B"
}
}
}
fun bind2() {
test.addObserver {
println("second $it")
}
}
fun start() {
bind1()
bind2()
test.value = "A"
}
After we call start function on iOS, in console we will see that observers calls in wrong order:
first A
first B
second B
second A
It happened because if we change value inside observer, changeValue function called recursively.
For workaround we can call observers with storedValue, as below
protected fun changeValue(value: T) {
storedValue = value
observers.forEach { it(storedValue) }
}
code sample
After we call start function on iOS, in console we will see that observers calls in wrong order:
It happened because if we change value inside observer,
changeValue
function called recursively. For workaround we can call observers withstoredValue
, as below