ArrayList 타입 LiveData를 사용하다 보면 element를 추가 하거나 제거 하는일도 필요할때가 있다.

val liveItems = MutableLiveData<ArrayList<String>>()
var items : ArrayList<String> = ArrayList()

items.add("1")
items.add("2")
items.add("3")

liveItems.value = items

ArrayList 였으면 add(), remove() 호출 뒤에 별도의 작업을 할필요 없지만 Databinding과 LiveData를 쓰게 되면 UI동기화를 위해 observer에게 notify를 해야 한다

그래서 setvalue , postvalue이러한 호출 통해서 작업 해야하는데 이러한것들이 너무나 귀찮고 복잡해진다.

그래서 add 나 remove호출 등은 임시의 List에서 처리하고 최종적인 결과만 LiveData로 넣는 방식이 있다

val liveItems = MutableLiveData<ArrayList<String>>()
var items : ArrayList<String> = ArrayList()

fun add(String item){
	item.add(item)
	liveItems.value = items
}

fun remove(String item){
	item.remove(item)
	liveItems.value = items
}

LiveData에 결과값만 덮어씌워주는 방식으로 처리하였다