MutatbleLiveData는 읽고 쓰기가 모두 가능하지만 LiveData는 읽기만 가능하다.
라이브데이터값을 뷰모델에서 변경해야되는데 외부에서 변경하지 않기 하기 위해 하는거다.
MutableLiveData를 사용하면 아래와 같이 MainActivity에서 값을 직접 수정할 수 있게 된다. 하지만 이렇게되면 프로그래머 입장에선 데이터관리가 복잡해져 유지관리하기가 어려워질 수 있다. 그래서 viewModel에서만 라이브데이터 값을 변경하게끔 설정하려면 LiveData를 사용하면된다.
class MainViewModel : ViewModel() {
var testMutableLiveData = MutableLiveData(0)
fun plusLiveDataValue(){
testMutableLiveData.value = testMutableLiveData.value!!.plus(1)
}
}
class MainActivity : AppCompatActivity() {
private lateinit var viewModel : MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// MainViewModel 과 연동
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
// 버튼을 클릭하면 viewModel에 있는 함수 실행(+1)
findViewById<Button>(R.id.btnArea).setOnClickListener {
viewModel.plusLiveDataValue()
}
// 뷰모델의 testMutableLiveData를 관찰해주고 UI에 반영
viewModel.testMutableLiveData.observe(this, Observer {
findViewById<TextView>(R.id.textArea).text = it.toString()
})
// mainActivity에서 값을 직접 수정할 수 있다.
viewModel.testMutableLiveData.value = 10
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textArea"
android:textSize="60dp"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnArea"
android:text="btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MutableLiveData를 사용하면 이와같이 직접적으로 MainActivity에서 데이터값을 수정할 수 있게된다.
class MainViewModel : ViewModel() {
private var _testMutableLiveData = MutableLiveData(0)
// _testMutableLiveData를 private 으로 선언하여 클래스 내부에서만 접근 가능하도록했다
// testLiveData의 타입은 LiveData이기때문에 testLiveData를 통해 관찰만 가능하고, 수정은 불가능하다
val testLiveData : LiveData<Int>
get() = _testMutableLiveData
fun plusLiveDataValue(){
_testMutableLiveData.value = _testMutableLiveData.value!!.plus(1)
}
}
class MainActivity : AppCompatActivity() {
private lateinit var viewModel : MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// MainViewModel 과 연동
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
// 버튼을 클릭하면 viewModel에 있는 함수 실행(+1)
findViewById<Button>(R.id.btnArea).setOnClickListener {
viewModel.plusLiveDataValue()
}
// 뷰모델의 testMutableLiveData를 관찰해주고 UI에 반영
viewModel.testLiveData.observe(this, Observer {
findViewById<TextView>(R.id.textArea).text = it.toString()
})
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textArea"
android:textSize="60dp"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnArea"
android:text="btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
LiveData를 사용하면 mainActivity에서 값을 직접 수정할 수 없다.