Martin Kim
Martin Kim

Reputation: 57

Kotlin coroutine + LiveData + DataBinding problem

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

Answers (3)

Martin Kim
Martin Kim

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

apksherlock
apksherlock

Reputation: 8371

Have you connected the Binding with your actual ViewModel?

someFragmentActivityBinding.viewModel = myViewModel

Upvotes: 0

Dak28
Dak28

Reputation: 259

Did you add this in you main activity?

binding.setLifecycleOwner(this)

Upvotes: 5

Related Questions