Rendy
Rendy

Reputation: 5698

MutableLiveData setValue does not execute next code

I have

var test : MutableLiveData<String> = MutableLiveData<String>("test")
...
somewhere callback fun from library {
    println("test 1")
    test.value = "123"
    println("test 2")
}

However only test 1 is printed in the log. Does anyone know this issue and how to solve this? I have checked that the callback runs on main thread though.

Note:

Issues only happens on that callback, it is fine if I .value in other code.

Upvotes: 0

Views: 117

Answers (2)

meStronger
meStronger

Reputation: 1

Try this

test.postValue("123")

It would help

Upvotes: 0

Rendy
Rendy

Reputation: 5698

Solved by I need to explicitly assign the value inside main thread:

Handler(Looper.getMainLooper()).post(Runnable {
    test.value = "123"
})

Upvotes: 0

Related Questions