2020/08 3

smart cast to is impossible because is a mutable property that could have been changed by this time

변경될 수 있는(mutable/var) 타입을 캐스팅 할 경우 문제가 생긴다. 이 경우에는 val 불변 타입으로 새로운 변수에 할당 한 뒤에 캐스팅을 해야 한다. private val plantDetailViewModel: PlantDetailViewModel by viewModels { InjectorUtils.providePlantDetailViewModelFactory(requireActivity(), args.plantId) } val binding = DataBindingUtil.inflate( inflater, R.layout.fragment_plant_detail, container, false ).apply { viewModel = plantDetailViewModel //error bea..

[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..

반응형