Android Jetpack

Android LiveData - 3 (Livedata vs MutableLiveData)

개복치 개발자 2022. 1. 2. 15:26

 지금까지 LiveData를 배운다고 했는데, mutableLiveData에 대해서만 알아봤습니다.

 

 LiveData와 MutableLiveData는 어떤 것이 다른지 한번 살펴보겠습니다.

 

 이름처럼 MutableLiveData는 변경이 가능한 값이고, LiveData는 변경이 불가능합니다.

 

 아니, 그러면 변경할 필요가 없는 데이터는 LiveData로 하고, 변경할 필요가 있는 데이터는 MutableLiveData로 하면 되나??

 

 라는 의문이 드는데, 꼭 그런건 아닙니다.

 

 구글에서는 어떻게 설명하고 있는지 살펴보겠습니다.

 

 구글에서는 캡슐화를 위해서 LiveData를 이용한다고 적혀있습니다.

 

Task: Encapsulate the LiveData

Encapsulation is a way to restrict direct access to some of an object's fields. When you encapsulate an object, you expose a set of public methods that modify the private internal fields. Using encapsulation, you control how other classes manipulate these internal fields.

In your current code, any external class can modify the score and word variables using the value property, for example using viewModel.score.value. It might not matter in the app you're developing in this codelab, but in a production app, you want control over the data in the ViewModel objects.

Only the ViewModel should edit the data in your app. But UI controllers need to read the data, so the data fields can't be completely private. To encapsulate your app's data, you use both MutableLiveData and LiveData objects.

 

작업: LiveData 캡슐화

캡슐화는 일부 개체 필드에 대한 직접 액세스를 제한하는 방법입니다. 개체를 캡슐화하면 개인 내부 필드를 수정하는 공용 메서드 집합이 노출됩니다. 캡슐화를 사용하여 다른 클래스가 이러한 내부 필드를 조작하는 방법을 제어합니다.

현재 코드에서 모든 외부 클래스는 value 속성을 사용하여 점수 및 단어 변수를 수정할 수 있습니다(예: viewModel.score.value 사용). 이 코드랩에서 개발 중인 앱에서는 중요하지 않을 수 있지만 프로덕션 앱에서는 ViewModel 개체의 데이터를 제어하려고 합니다.

ViewModel만 앱의 데이터를 편집해야 합니다. 그러나 UI 컨트롤러는 데이터를 읽어야 하므로 데이터 필드가 완전히 비공개일 수는 없습니다. 앱의 데이터를 캡슐화하려면 MutableLiveData 및 LiveData 개체를 모두 사용합니다.

 

 요약하자면, 캡슐화를 위해 사용하고, ViewModel에서만 앱의 데이터를 수정하기 위해서 사용한다

 

 라고 적혀있습니다.

 

 조금 더 읽어볼까요?

 

MutableLiveData vs. LiveData:

Data in a MutableLiveData object can be changed, as the name implies. Inside the ViewModel, the data should be editable, so it uses MutableLiveData.
Data in a LiveData object can be read, but not changed. From outside the ViewModel, data should be readable, but not editable, so the data should be exposed as LiveData.

 

MutableLiveData 대 LiveData:

MutableLiveData 개체의 데이터는 이름에서 알 수 있듯이 변경할 수 있습니다. ViewModel 내부에서 데이터는 편집 가능해야 하므로 MutableLiveData를 사용합니다.
LiveData 개체의 데이터는 읽을 수 있지만 변경할 수는 없습니다. ViewModel 외부에서 데이터는 읽을 수 있지만 편집할 수 없어야 하므로 데이터가 LiveData로 노출되어야 합니다.

 

 그러니깐 ViewModel 내부에서 데이터가 편집 가능해야 하고, 외부에 노출되는 데이터는 LiveData여야 한다. 

 

 라고 적혀있습니다.

 

 자, 그렇다면 코드로 한번 보겠습니다.

 

 아래 글에 기존에 작성했던 코드에서 변경해보겠습니다.

 

https://philosopher-chan.tistory.com/1475

 

Android LiveData - 2 (ViewModel + LiveData)

 이 글에서 Livedata에 대해 잠시 봤는데, 이 것을 ViewModel을 이용해서 한번 보겠습니다.  아래와 같이 ViewModel을 만들어줍니다.  그냥 단순하게, Livedata부분을 ViewModel로 옮겨준 것이라고 생각하면

philosopher-chan.tistory.com

 

 여기에서 ViewModel의 구조는 똑같습니다.

 

 코드를 바꿔주면 아래와 같이 동작하는 것을 볼 수 있습니다.

 

 

 

 다음에는 LiveData에 대해서 더 알아보겠습니다.

 

 

 - 참조

 

https://developer.android.com/codelabs/kotlin-android-training-live-data?index=..%2F..android-kotlin-fundamentals&hl=fi_FI#5 

 

https://developer.android.google.cn/codelabs/kotlin-android-training-live-data/index.html?index=..%2F..android-kotlin-fundamentals&hl=IT#2