ViewModel 3

Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option

Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option viewModel 에 param을 추가하였더니 viewModel Factory가 필요하고, 추가 하니 위와 같은 에러가 나왔다~ 버전 충돌 문제인데~ build.gradle.app 에 kotlinOptions { jvmTarget = '1.8' freeCompilerArgs += [ '-Xjvm-default=enable' ] } 또는 tasks.withType(KotlinCompile).configureEach { kotlinOptions { freeCompilerArgs += [ "-Xjvm-default=all", ] } }..

LiveData 와 MutableLiveData

MutableLiveData는 Abstract Class 인 LiveData를 구현한 Public Class 위 말을 풀어 보면, 1. LiveData는 추상 클래스이므로 직접 생성할 수 없다. public abstract class LiveData { protected void postValue(T value) {} @MainThread protected void setValue(T value) {} //데이터 수정은 pretected 메서드를 구현해서 사용해야 한다. } val name : LiveData = LiveData() 위와 같이 직접 생성할 경우 아래와 같이 에러가 발생한다. cannot create an instance of an abstract class 반면에 MutableLiveDa..

[MVVM] LiveData와 ViewModel Observer로 연결

Repository의 Data가 변경 되었을 때 UI에 알리기 위해서는 ViewModel이 관찰을 하고 있어야 한다. UI(V)는 보통 onCreate()에서 VM을 생성하고, VM은 Repository를 생성한 뒤 M과 Dao를 통해 연결 되는데, 이 Dao를 LiveData로 구성하면 ViewModel이 지켜 볼 수(obsever) 있게 된다. 연결(MVVM) onCreate/onCreateView 실행이 되면서 viewModel 생성 작업과 이후 연결 작업이 실행 된다. Fragment.kt private val plantDetailViewModel: PlantDetailViewModel by viewModels { InjectorUtils.providePlantDetailViewModelFacto..