未调用 CoroutineLiveData Builder 存储库

CoroutineLiveData Builder repository not being invoked

我正在尝试使用引用 here 的新 liveData 构建器来检索我的数据,然后将其转换为视图模型。但是,我的存储库代码没有被调用(至少我在使用调试器时看不到它被触发)。我不应该使用两个 liveData{ ... } 生成器吗? (一个在我的存储库中,一个在我的视图模型中)?

class MyRepository @Inject constructor() : Repository {

    override fun getMyContentLiveData(params: MyParams): LiveData<MyContent> =
    liveData {

        val myContent = networkRequest(params) // send network request with params
        emit(myContent)
    }
}


class MyViewModel @Inject constructor(
    private val repository: MyRepository
) : ViewModel() {

    val viewModelList = liveData(Dispatchers.IO) {
        val contentLiveData = repository.getContentLiveData(keyParams)
        val viewModelLiveData = contentToViewModels(contentLiveData)
        emit(viewModelLiveData)
}

    private fun contentToViewModels(contentLiveData: LiveData<MyContent>): LiveData<List<ViewModel>> {
        return Transformations.map(contentLiveData) { content ->
            //perform some transformation and return List<ViewModel>
        }
    }
}

class MyFragment : Fragment() {

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory
    val myViewModel: MyViewModel by lazy {
        ViewModelProviders.of(this, viewModelFactory).get(MyViewModel::class.java)
    }

    lateinit var params: MyParams

    override fun onAttach(context: Context) {
        AndroidSupportInjection.inject(this)
        super.onAttach(context)
        myViewModel.params = params
        myViewModel.viewModelList.observe(this, Observer {
            onListChanged(it) 
        })

    }

您可以尝试 emitSource:

val viewModelList = liveData(Dispatchers.IO) {
    emitSource(
        repository.getContentLiveData(keyParams).map {
            contentToViewModels(it)
        }
}