Android Paging 3 library - for paging data from Room database, flow becomes empty on data change

2.9k views Asked by At

I am trying to use android Paging 3 (version 3.0.0-SNAPSHOT) for paging data from Room database(no remote data source).

Initially the page loads data successfully , but when a new "Entry" is added to database and I return to this page,collectLatest is fired but no data is loaded ("pagingData" entry list is empty )

this is my query :

   @Query("SELECT * FROM entry ORDER BY dateTime DESC")
  fun getAll() : PagingSource<Int, Entry>

and this is my viewmodel:

 val flow = Pager(
    PagingConfig(pageSize = 20)
) {
    entryDao.getAll()
}.flow
   .cachedIn(viewModelScope)

in my fragment , I am observing the data like this :

iewLifecycleOwner.lifecycleScope.launch {
        homeViewModel.flow.collectLatest { pagingData ->
            adapter.submitData(pagingData)
        }
    }

and this is my adapter:

class EntriesAdapter( val context : Context, private val onClick: (String)->Unit) :
    PagingDataAdapter<Entry , RecyclerView.ViewHolder>(diffCallback)
{
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EntryViewHolder
            = EntryViewHolder(
     DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_entry, parent, false
        ) , onClick)

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var entry = getItem(position)
        if (entry != null) {
            (holder as EntryViewHolder).bindTo(entry);
        }
    }

    companion object {
        //This diff callback informs the PagedListAdapter how to compute list differences when new
        private val diffCallback = object : DiffUtil.ItemCallback<Entry>() {
            override fun areItemsTheSame(oldItem: Entry, newItem: Entry): Boolean =
                oldItem.id == newItem.id

            override fun areContentsTheSame(oldItem: Entry, newItem: Entry): Boolean =
                oldItem.id == newItem.id
        }
    }
    inner class EntryViewHolder( val binding : ItemEntryBinding ,val onCLick:
        (String)->Unit ) : RecyclerView.ViewHolder(binding.root) {
        var entry:Entry? = null
        fun bindTo(entry: Entry) {
            this.entry = entry
            with(binding) {
                entryItem = entry
                cardView.setOnClickListener{
                    onCLick
                }
              
                executePendingBindings()
            }
        }
    }
}
1

There are 1 answers

0
skajar On

I had a similar problem to yours and I just forgot to call entriesAdapter.refresh() after add new item.