1
votes

Suppose, I've a MutableLiveData<User>. So if I update a variable in User let say userName using the value like

var user = MutableLiveData<User>()
user.postValue(User())
user.value.userName = "ABC"

it won't call the observer. So I have to call a postValue to invoke it. Is there any to invoke observer after updating one property of MutableLiveData object.

1
Livedata working based on the object you specified, here it is User and based on the attributes of user object - Shalu T D
here you update the property of MutableLiveData not the User. You could do something like user.postValue(User().apply{userName = "ABC"}), or instantiate User( )before, modify the property and then post it - Stachu

1 Answers

0
votes

At the end @Stachu solution works but, I have made a change by passing the value of apply block in the postValue() like this -

user.postValue(user.value.apply{ userName = "abc" }.value)