I’ve a list as part of state (data class) I’m main...
# android
k
I’ve a list as part of state (data class) I’m maintaining inside a viewModel. This state is being observed by the UI components and updated accordingly. I also want to be able to edit/delete an element from this list but I can’t make the list mutable (As UI is supposed to consume it as immutable list) so everytime I need to do any modifications on that list, I go through
Copy code
val newEntries = currentState().subEntries.toMutableList()
newEntries.remove(action.subEntry)
setState { copy(subEntries = newEntries.toList()) }
Are there any performance hits by doing it this way?
e
Wouldn't you be able to simplify that to the following?
Copy code
setState { state.copy(subEntries = state.subEntries - action.subEntry) }
I.e.: use the minus operator on the existing list, which returns a new list
k
Makes sense. I was concerned whether creating new list everytime is okay?
e
The minus operator also creates a new list, so it all depends on how often this code is called.