Use MediatorLiveData To Query And Merge Multiple Data Source Type Into Single LiveData (Kotlin)

Assuming I need to fetch a list of Password and Category, merging them into a single LiveData.

First, I create a Sealed Classes to represent both data type.

sealed class MergedDatadata class PasswordData(val passwordItems: List<Password>): MergedData()data class CategoryData(val categoryItems: List<Category>): MergedData()

Then I create a MediatorLiveData to fetch both data type.

fun fetchData(): MediatorLiveData<MergedData> {    val liveDataMerger = MediatorLiveData<MergedData>()    liveDataMerger.addSource(passwordDataSource.fetchAll(), {        if (it != null) {            liveDataMerger.value = PasswordData(it)        }    })    liveDataMerger.addSource(categoryDataSource.fetchAll(), {        if (it != null) {            liveDataMerger.value = CategoryData(it)        }    })    return liveDataMerger}

Lastly I observe the LiveData and make sure both data sets are received before processing.

val liveData = fetchData()var passwordItems: List<Password>? = nullvar categoryItems: List<Category>? = nullliveData.observe(activity, object : Observer<MergedData> {    override fun onChanged(it: MergedData?) {        when (it) {            is PasswordData -> passwordItems = it.passwordItems            is CategoryData -> categoryItems = it.categoryItems        }        if (passwordItems != null && categoryItems != null) {            // both data is ready, proceed to process them            // stop observing            liveData.removeObserver(this)        }    }})  

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.