Zhuinden/realm-monarchy

On Database update Datasource is getting invalidated and as a result of this new paged list is getting created and It refresh the UI

Closed this issue · 7 comments

Scenario:

  1. Assume in database already item from 0 to 100 is there.
  2. I started scrolling in the list. On scroll end- I am making an API call to fetch the next list of item from 100 to 200
  3. After that insert these items into the Database.

realmLivedataWrapper.doWithRealm { realmObj -> realmObj.executeTransaction { it.insertOrUpdate(mails.mails) // Todo make change } }

  1. This eventually call RealmChangeListener<RealmResults<T>>.onChange
  2. Which invalidate the db in PagedLiveResults
  3. As a result of this, PagedList is getting recreated and scroll back to the top of the list.

PagedList is backed by a PositionalDataSource, which would be retaining the last index and therefore only reload the page where you currently scrolled, but not jump up. Otherwise even Room's DataSource.Factory would make the RecyclerView jump up, it works by the same principle.

But I'll try to look at this later, I think in your case the problem is more likely that you reset the adapter each time there is a change, and not because the data source is invalidated.

Adapter resetting is not happening at the time of change. But RealmChangeListener.onChanged is calling the PagedLiveResults.updateResults which trigger dataSource.invalidate()

Not sure is that what happens in Room also
https://issuetracker.google.com/issues/123834703

But RealmChangeListener.onChanged is calling the PagedLiveResults.updateResults which trigger dataSource.invalidate()

That is how Paging intends to work and how it works with Room, so that in itself shouldn't be a problem. Do you have placeholders enabled or disabled?

Placeholder is disabled.

Have you tried enabling it?

Thank you so much. After enabling it is working fine :). Can you please tell me what is the reason it was not working when it was disabled?

Interesting question, I guess positional data sources require placeholders to work reliably.

If placeholders are disabled, not-yet-loaded content will not be present in the list. Paging will still occur, but as items are loaded or removed, they will be signaled as inserts to the PagedList.Callback. onChanged(int, int) will not be issued as part of loading, though a PagedListAdapter may still receive change events as a result of PagedList diffing.

I guess that means that the positions will be off as there is no null in place of unloaded items. The docs say that the default setting for placeholders is true.

With all that in mind, I've updated the Paging example in Monarchy to explicitly enable placeholders so this doesn't happen again (52c29c7).

Thanks!