Observe not called with LiveData<PagedList> when we start observing the LiveData before LivePagedListBuilder() is called

189 views Asked by At

I'm using the PagedList architectural components using a custom Paged datasource and I am having trouble returning results to the observe method.

below doesn't work:

fragment onviewcreated method

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  viewModel?.searchDetails?.observe(viewLifecycleOwner, {
                populateSearchResults(it)
            }) 
}

viewmodel code where I update livedata from LivePagedListBuilder upon a user click event(not in viewmodel's init(){} method)

fun onProcessSearch(vararg searchValue: Array<String?>?) {
        val pagedListConfig = PagedList.Config.Builder()
                .setEnablePlaceholders(true)
                .setPageSize(PageSize).build()

        searchDetails = LivePagedListBuilder(PageKeyedSearchDataFactory(viewModelScope, searchValue[0]), pagedListConfig).build()
    }

below works,

just moving the observe part after calling the above view model function works, Fragment code:

 override fun onClick(view: View) {
viewModel?.onProcessSearch(searchValue)

if (view.id == R.id.match_patient_search_button) {
  patientDetailsViewModel?.patientSearchAddedDetails?.observe(viewLifecycleOwner, {
                populateSearchResults(it)
                GlobalHelper.hideKeyBoard(view)
            })
  }
}

Could anyone help why the above works, because as per doc we should observer livedata on onViewCreated method.

0

There are 0 answers