'How to add an item to a list of LiveData

I want to know how to add an item to a List in LiveData and observe it.

I've seen other similar questions, and in response to them, I've seen managing list and LiveData respectively and using them.

For example,

class WorkoutRepository() {
    val setInfoList = ArrayList<WorkoutSetInfo>()
    var setInfoLiveData = MutableLiveData<List<WorkoutSetInfo>>()

    fun add(item: WorkoutSetInfo) {
        setInfoList.add(item)
        setInfoLiveData.postValue(setInfoList)
    }
}

like this.

After adding items to the list like this, the list itself was valued in LiveData.

Every time an item is added or deleted, the entire List has to be revalued, so I think it will be inefficient in terms of performance.

So I was wondering if there is no way to add items to LiveData at once.

Or does the performance not matter at all if I value the entire List every time?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source