android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.36k stars 8.28k forks source link

PagingWithNetworkSample: After Paging 3 update, indexInResponse is now unused; Ordering behavior is unclear #1051

Closed allanconda-mercari closed 1 year ago

allanconda-mercari commented 2 years ago

I'm trying to understand how to keep the ordering from the backend into Room, and I noticed the variable indexInResponse was used for this exact use case. However, after the paging 3 migration in this codebase, this variable is now unused and returns 0 for all items, although it is still used in ORDER BY in the query.

I need to do exactly this so I want to know whether insertion order is preserved now in Paging 3 making indexInResponse unnecessary. From checking Room-generated I don't see anything like this happening, and as far as I know, SQLite doesn't guarantee this, otherwise, a variable like indexInResponse would not be necessary in the first place even for Paging 2.

Looking for clarification in this scenario, so comments around this are appreciated.

claraf3 commented 1 year ago

By default, ordering is not guaranteed. In the sample's case, Reddit backend does not guarantee order therefore the app maintains order with indexInResponse which is based on insertion order.

The indexInResponse logic is broken after updates but I have updated the sample app to track insertion order with Room's autoGenerated Int PrimaryKey. On the query to return PagingSource, it will need to ORDER BY _id ASC to ensure PagingSource always returns data in the same order.