Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LiveData Paged List size is always 0

I implemented Paging using android paging library. The ViewModel returns a LiveData<PagedList<MyObject>> and I observe that in the fragment where I set the adapter and everything is working good, except that when I want to check the size of the list is always 0.

viewModel.getSearchResults().observe(this, mList -> {
           adapter.submitList(mList);
           Log.e("Count", String.valueOf(mList.size()));
       });

Where mList is a PagedList<MyObject>

like image 921
max Avatar asked Jun 24 '19 19:06

max


People also ask

Is the data in a livedata object set?

Initially, the data in a LiveData object is not set. To avoid bloated activities and fragments. Now these UI controllers are responsible for displaying data but not holding data state. To decouple LiveData instances from specific activity or fragment instances and allow LiveData objects to survive configuration changes.

When does livedata receive the most recent value?

As soon as an app component is in the STARTED state, it receives the most recent value from the LiveData objects it’s observing. This only occurs if the LiveData object to be observed has been set. Generally, LiveData delivers updates only when data changes, and only to active observers.

What is the size of the pagedlist?

Without null placeholders, the PagedList is the sublist of data that has already been loaded. The size of the PagedList is the number of currently loaded items, and get (N) returns the N th loaded item.

How do I represent not-yet-loaded data in pagedlist?

There are two ways that PagedList can represent its not-yet-loaded data - with or without null placeholders. With placeholders, the PagedList is always the full size of the data set. get (N) returns the N th item in the data set, or null if its not yet loaded.


Video Answer


1 Answers

If you look at Loading Data from PagedList documentation you'll notice the following:

If you use LivePagedListBuilder to get a LiveData<PagedList>, it will initialize PagedLists on a background thread for you.

Also, Mutability and Snapshots states the following:

A PagedList is mutable while loading, or ready to load from its DataSource. As loads succeed, a mutable PagedList will be updated via Runnables on the main thread. You can listen to these updates with a PagedList.Callback. (Note that PagedListAdapter will listen to these to signal RecyclerView about the updates/changes).

If you want to listen for the events onInserted, onChanged or onRemoved you can do the following:

viewModel.observableData.observe(viewLifecycleOwner, Observer { pagedList ->
    adapter.submitList(pagedList)
    pagedList.addWeakCallback(null, object: PagedList.Callback() {
        override fun onChanged(position: Int, count: Int) {}
        override fun onInserted(position: Int, count: Int) {
            println("count: $count")
        }
        override fun onRemoved(position: Int, count: Int) {}
    })
})
like image 82
Rodrigo Queiroz Avatar answered Sep 28 '22 07:09

Rodrigo Queiroz