Livedata setvalue is ignored when called multiple times

In the following code only functionC is invoked. getValues method is called from Main thread from a fragment, but function A and FunctionB are skipped

class MyViewModel() {
fun getValues(){
_livedata.value = SomeFuctionA()
_livedata.value = SomeFuctionB()
_livedata.value = SomeFuctionC()
}
}

However if I modify the method to launch explicitly from main thread, all functions are called

class MyViewModel() {
fun getValues(){
viewmodelScope.launch(Dispathers.Main){
_livedata.value = SomeFuctionA()
_livedata.value = SomeFuctionB()
_livedata.value = SomeFuctionC()
}
}
}

This is not making sense because getValues is invoked from MainThread in both cases, however all functions are observed only in second case. How to ideally make sure livedata is observed for all three functions ?

4

You can call it on any thread with all of them executing just like below code

_livedata.value = SomeFuctionA()
joinAll()
_livedata.value = SomeFuctionB()
joinAll()
_livedata.value = SomeFuctionC()

You can only use joinAll() in a coroutine or a suspend function. Keep that in mind.

Also a little extra, if you want some delay when being executed add below code

delay(10.seconds)

Let’s discuss about your first code snippet in which three functions values are getting saved in _livedata, here whats happening is that data of functionA is getting overridden by functionB and then by functionC. And it’s happening so fast that it’s looking to you that functionA and functionB is getting skipped, because functionC is overriding the data very fast.

Now, discussing about your second code snippet, here you have used the coroutine and because of that coroutine are getting created and delaying the result of function, functionB and functionC. Because of that you are able to get the result, but it’s still wrong as it will override the data at the end. Your code is also not maintaining the right coding standard in this scenario. 


So, your both code snippets are wrong(because -> setting the value “_livedata.value=” will replace old data with new, its a feature of liveData).

Solution

You have multiple ways to solve this problem.



  1. Use a list to emit all the results together.

    You can modify your liveData to emit a list of results(from all functions) rather than just a single result (from only one function).

In this way observer will receive a list containing all the data of functionA, functionB, functionC.

class MyViewModel : ViewModel() {
private val _livedata = MutableLiveData<List<ResultType>>() // Assume ResultType is the type returned by your functions
val livedata: LiveData<List<ResultType>> = _livedata

fun getValues() {
    // Collect all the results
    val results = listOf(
        SomeFunctionA(),
        SomeFunctionB(),
        SomeFunctionC()
    )

    // Emit the entire list
    _livedata.value = results
}

}

Observation in fragment

viewModel.livedata.observe(viewLifecycleOwner, Observer { results ->
results.forEach { result ->
    // Process each result individually
}

})

  1. Using MediatorLiveData.

if you have used this before you know that when data is coming from
multiple sources then mediatorLiveData is best to use as it add all
the data from different sources and provide you the result.



If you have not tried mediatorLiveData below is the official page of
it.

mediatorLiveData

first create different liveData for each functions, which will allow you to handle the liveData separately.

Working method:

class MyViewModel : ViewModel() {
private val _liveDataA = MutableLiveData<ResultType>()
private val _liveDataB = MutableLiveData<ResultType>()
private val _liveDataC = MutableLiveData<ResultType>()

// Use MediatorLiveData to combine all LiveData results
val combinedLiveData = MediatorLiveData<List<ResultType>>().apply {
    val resultList = mutableListOf<ResultType>()

    // Add sources for each LiveData
    addSource(_liveDataA) { resultA ->
        resultList.add(resultA)
        value = resultList.toList()  // Update the MediatorLiveData with the combined result
    }

    addSource(_liveDataB) { resultB ->
        resultList.add(resultB)
        value = resultList.toList()
    }

    addSource(_liveDataC) { resultC ->
        resultList.add(resultC)
        value = resultList.toList()
    }
}

// Function to get values and update individual LiveData objects
fun getValues() {
    _liveDataA.value = SomeFunctionA()
    _liveDataB.value = SomeFunctionB()
    _liveDataC.value = SomeFunctionC()
}

}

MediatorLiveData observes all the three different liveData source(_liveDataA, _liveDataB, _liveDataC) and each time any function (A, B,C ) updates its respective liveData, mediatorLiveData will combines the result into a list and update it to the observer.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật