1

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.

Ashutosh Sagar
  • 981
  • 8
  • 20
  • Livedata working based on the object you specified, here it is User and based on the attributes of user object – Shalu T D Aug 07 '20 at 10:07
  • please check this https://stackoverflow.com/questions/48020377/livedata-update-on-object-field-change – Shalu T D Aug 07 '20 at 10:10
  • 1
    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 Aug 07 '20 at 10:11

1 Answers1

0

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)
Ashutosh Sagar
  • 981
  • 8
  • 20