I\'m using Android MVVM architecture with LiveData. I have an object like this
public class User {
private String firstName;
private String lastName;
If you are using Kotlin and LiveData, I can offer you 2 ways - with and without extension fucntion:
liveData.value = liveData.value?.also { it ->
// Modify your object here. Data will be auto-updated
it.name = "Ed Khalturin"
it.happyNumber = 42
}
// Extension. CopyPaste it anywhere in your project
fun MutableLiveData.mutation(actions: (MutableLiveData) -> Unit) {
actions(this)
this.value = this.value
}
// Usage
liveData.mutation {
it.value?.name = "Ed Khalturin"
it.value?.innerClass?.city= "Moscow" // it works with inner class too
}