android LiveData or coroutines channel

空扰寡人 提交于 2020-07-10 09:34:56

问题


Having app using LiveData with ViewModel for UI to observe the data update in the repository.

It is working fine. Now someone brought it up the "LiveData has not been well adopted, maybe it should be switch to use coroutines' channel".

First of all not sure the statement about LiveData is accurate or not. I am sure with coroutines' feature it could be done without LiveData. But I feel every one has its targeted task, and from the speech/sample of Google the LiveData is built with Android architecture component and is used for the cases like providing a live data channel between data repository and the UI.

The channel is a language feature from kotlin. Sure it could be used in many cases. I just hope it is not because it has named "channel" so people feel that is more suitable than using LiveData here.

A not very proper sample is, could the message bus/event queue be also right one for being used in where the LiveData is used? They can also be subscribed/observed.

Just dont have a very strong evidence to see either LiveData is better to use in this case or coroutines channel is better, not knowing the channel very well.

Anyone care to share some thought?s


回答1:


LiveData and Kotlin coroutines are pretty different concepts. Actually, you should use both. LiveData binds the UI with a data, no matter where the data comes from. Coroutines is alternative to threads, it's just a better way to deal with concurrency.

LiveData and coroutines don't depend on each other, but the concepts are great, and it's better to use them both. Look at liveData coroutine builder function, which helps to gracefully join the both concepts. See Use Kotlin coroutines with Architecture components for more details.




回答2:


You can use both together, Live data works on observer pattern to provide you binding between UI and data repository. Coroutines are for background processes like to load data from db or to fetch data from an api. But the problem was how to notify UI or return Kotlin coroutines functions result to UI thread after successful completion. Currently, most of us was achieving this by introducing backing property (create separate Mutable field and pass in the immutable field getter)

But now with the support of Kotlin extension (LifecycleScope) we can easily connect LiveData with Coroutine.

androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01 or higher.

Like this is a function which is using Coroutine and returning a LiveData

 /**
 * Get all news rows livedata pageList from DB using Coroutine.
 */
suspend fun getAllNewsLiveData(): LiveData<PagedList<News>> {
    return withContext(Dispatchers.IO) {
        val data = mDao.getAllNews()
        LivePagedListBuilder(data, Constants.PAGINATION_SIZE).build()
    }

}

Now in UI class we can simply call this function using lifescope extension

  lifecycleScope.launchWhenStarted {
        newsViewModel.getNews()?.observe(this@NewsActivity, Observer { pagedNewsList -> pagedNewsList.let { newsAdapter.submitList(pagedNewsList) } })
    }


来源:https://stackoverflow.com/questions/57502970/android-livedata-or-coroutines-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!