LiveData is not getting observed for one specific scenario

1k Views Asked by At

I have 3 LiveData objects in my ViewModel, I'm applying transformations to these, the problem is 2 LiveData are getting observed while the other one is not, I've tried different solutions like changing the way ViewModel is initialized or the way LiveData is initialized but nothing has worked for me.

class MyClass : ViewModel() {

    init {
        _originallist.value = Instance.getOrignalList()
    }
     // observed in Fragment A
    val a: LiveData<List<A>> = Transformations.map(_searchText, ::extractA)
     // observed in Fragment B
    val b: LiveData<List<B>> = Transformations.map(_originallist, ::extractB)
     // observed in Fragment C
    val c: LiveData<List<C>> = Transformations.map(_originalList, ::extractC)

     // Called from Fragment D to change the _searchText liveData
    fun setSearchText(text: String) {
        _searchText.value = text
    }

    fun extractA(text: String): List<A> {
        val temp = ArrayList<A>()
        list.foreach {
            if (it.contains(text, false) temp . add (it)
        }
        return temp
    }

    fun extractB(list: List<B>): List<B> {
        // do something
    }

    fun extractC(list: List<C>): List<C> {
        // do something
    }
}

If you have noticed that the LiveData b and c are getting initialized just once hence I'm able to see the data in my RecyclerView, but for the LiveData A, the search text can change based on user input, this is where my fragment is not observing this live data. Things to note: This is a common ViewModel for my 3 viewPager fragments, LiveData a is observed in one fragment, B in another and C in another. Eventually, I have to apply the search for other 2 fragments as well. When I was debugging the observer lines in my fragment was getting skipped, another thing I would like to point out is that the code in all 3 fragments is same except that they are observing different LiveData

EDIT: What i have noticed now is that, since i'm calling the setSearchText() from Fragment D i'm able to observe the changes of LiveData A in Fragment D but i want to observe that in Fragment A but not able to. I have a search bar in fragment D and bottom of that i have a view pager with 3 fragments, all 4 fragments have a common viewModel, Am i doing something wrong here or is this not the way to implement this? TIA

2

There are 2 best solutions below

0
On BEST ANSWER

Finally found the root cause, the problem was that the viewModel was getting its own lifecycle owner in each of fragment, the solution to this was to declare and initialize the viewModel object in the parent activity of the fragments and use its instace in the fragment to observe the LiveData

1
On

The problem is:

Your function extractA in

val a: LiveData<List<A>> = Transformations.map(_searchText, ::extractA)

will only be executed when the value of _searchText will change.

That's how Transformations work, they apply the given function whenever the value changes.