Realm Sync Pagination

It is been weeks trying to figure how to use Pagination using Realm Sync or Data API (Preview). Nothing Works! Please help with any type of Document on how to Paginate using Realm Sync as i am doing an E-Commerce Application and in need of Pagination as i cannot have many requests to show products. Appreciate the help. Note i am coding Either Java or Kotlin.

Thank you.

Hi, Realm Sync does not support pagination. The ideal way to accomplish this would be to sync down the result set you are looking for and then just using the Realm query language to get the subset of data that is in your local database. The reason for this is that syncing on a paginated set of data is a bit of an anti-pattern as its just emulating a REST client but with a lot more overhead and uncertainty about what it means to sync a paginated set of data (if something is inserted in the beggining of the result set should we sync that?).

If you cannot sync down the entire query of data, then perhaps you can use GraphQL or the Remote MongoDB client of the SDK: https://www.mongodb.com/docs/realm/sdk/node/examples/query-mongodb/

Thanks to lazy evaluation, the common task of pagination becomes quite simple. For example, suppose you have a results collection associated with a query that matches thousands of objects in your realm. You display one hundred objects per page. To advance to any page, simply access the elements of the results collection starting at the index that corresponds to the target page.

This is an answer on Realm Document, however is there any Example on this!

You can use Sort and Limit to perform pagination on results that are already in a realm: https://www.mongodb.com/docs/realm/reference/realm-query-language/#sort--distinct--limit

So, i should ignore the fact i have large data and just use Realm Sync? which is the same as Realm Results right?

Its up to you. You can either sync down all of the data and just paginate locally (should make for very fast scrolling once the data has been synced down) or you can use the MongoDB Query endpoing in the SDK’s which go through Realm Cloud or GraphQL if you want it to be more like a REST client.

Do you know roughly how much data you are talking about here?

Lets say i have a Recycler View that will have products that show 500 to 1000 products? how would i go from there? i would appreciate the help of any kind.
I am also looking to filter these products depending on client selections such as Color, Size, etc…

Thank you.

Like I said above, it really just depends on your applications constraints and the size of your data. If its just 1000 objects/documents that are all medium sized I imagine, and you dont have any major networking constraints or storage constraints (if this is a normal mobile phone then you shouldn’t be too worried about this amount of data), then the ideal solution would be to sync down all 1000 of the documents and then have your application’s view controller read from the synced realm and perform whatever filtering you would like. This will provide a low-latency experience when using the application because when the user is scrollong, adding a new filter, etc, no new data needs to be requested from the server as it is all already stored on the device.