0

I have a recyclerview adapter that I update from my activity but the issue is whenever the live data object gets updated and I update the adapter the object I have in the adapter also gets the same data even I didn't assign updated value to it, this is pretty strange I didn't understand what is going on

Here is the adapter code where it set my data

fun setDevices(tempDevices: List<DataModelDevice>) {
    val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(
        DeviceDiffCallback(
            this.devices,
            tempDevices
        )
    )

    diffResult.dispatchUpdatesTo(this)
    this.devices = tempDevices
}

the issue is when I came to this block of code both the tempDevices and devices have the same data set and I don't understand why is the devices object gets updated even before I update it with tempDevices

Here is the code where i observe the changes in devices

    viewModel.devices.observe(this, {
        if (it.size != 0) {
            deviceAdapter.setDevices(it)
            binding.recyclerViewDevices.scrollToPosition(0)
        }
    })

This is my live data object in my viewModel class

var devices: MutableLiveData<MutableList<DataModelDevice>> = MutableLiveData()
Zubair Akber
  • 2,760
  • 13
  • 30
  • How do you create `tempDevices`? Is it possible that the old devices list and the new one are actually the same list? And just content of the list is changed? – Demigod Sep 14 '21 at 15:18
  • Why is your LiveData type a MutableList instead of read-only List? If you're submitting the same list back to the LiveData over and over, it can't be compared with itself. Also, instead of using DiffUtil manually, I think it would be easier to use ListAdapter and `submitList()`. – Tenfour04 Sep 14 '21 at 17:06

0 Answers0