Reputation: 57
I have a issue using Kotlin coroutine + LiveData + DataBinding problem.
My code is below
class TempViewModel: ViewModel() {
val creatorInfo: LiveData<CreatorInfo> = liveData(context = viewModelScope.coroutineContext + Dispatchers.IO) {
val data = CreatorInfoSettingRepository.requestCreatorInfo().body()
emit(data!!)
}
}
and xml use databinding like this
<TextView
android:text="@{viewModel.creatorInfo.email}" />
<TextView
android:text="@{viewModel.creatorInfo.phone}" />
....
I checked that fetching data(CreatorInfo) from server(retrofit2) is successful, but data is not applied to UI by databinding.
also when check observing like below, observe block is invoked.
viewModel.creatorInfo.observe(fragment, Observer { creatorInfo ->
Log.d("ssong","test")
})
Anyone who can help?
Upvotes: 2
Views: 932
Reputation: 57
There was my mistake. It works well. There was code that hide TextViews..(View.gone) (So I thought databinding does not work well).
Problem is solved. Thank you guys.
Upvotes: 0
Reputation: 8371
Have you connected the Binding
with your actual ViewModel
?
someFragmentActivityBinding.viewModel = myViewModel
Upvotes: 0
Reputation: 259
Did you add this in you main activity?
binding.setLifecycleOwner(this)
Upvotes: 5