Hi everyone :wave: I have an API that returns a li...
# android
e
Hi everyone 👋 I have an API that returns a list of 2000+ items in one response. Right now the UI is not laggy, but I’m worried it might lag on older or low-end devices. I’m using Jetpack Compose + MVVM and I’d like to add paging, but the server does not support pagination. What’s the best way to handle this? • Keep everything in memory and page it locally? • Save the data to Room and use a
PagingSource
?
a
Usually you would use a LazyList or UI component that recycles views to ensure you are only dealing with the visible number of items. As for the data itself, if you are worried about that getting big I would write to file/db/anything you choose, but also page that into memory based on the last visible item index in the result set. https://developer.android.com/develop/ui/compose/lists#lazy
m
As already mentioned by @Anonymike, when using lazy composables, you shouldn't be worried about the total item count as items are only rendered as required. Other considerations are: • For such request, the payload byte count is non-trivial. This can be slow to perform in poor connectivity situations, and can put load on the server. • Check the memory footprint of retrieving, deserializing, and handling the list. It is fine to work on a list of 2000 items or so, but it depends on the size of each item and the complexity of the models used to store these Handling the first point requires using local data storage, caching the data to it, and only reperforming the request when needed. In this case, your source of truth would be your Room database and you can use pagination as needed. Anyway, since the server doesn't support pagination, there would always be a workflow in your app that would deal with the data as a bulk, so handle it carefully.
🙌 1
a
@Mohamed Darwish makes a great point on having to handle the initial data load in bulk. I highly recommend looking at stax or streaming parsers in jackson or kotlinx serialization so you aren't reading the entire json at one time and can process the data item by item.
💯 1
s
• Save the data to Room and use a
PagingSource
? ^^^ This sounds like the way to go! + other extra recommendations given above will make it bullet proof.
c
You missed option 3: • Discuss with the backend team about implementing pagination on the API
👍 2
😂 8
a
True, but its possible he needs to store the data or view it offline for a reason. All that said, I would hope that's already been done before coming here haha.
e
Thanks a lot, this discussion really helped! I had already talked to the backend about pagination(But short answer no time ..... 😄 ), and I implemented a database for offline-first support. Appreciate your time and answers 🙂
u
@Eslam Hussein using DB not optimize solution it’s complexity for for 2000+ initial insert cost also sync / migration is overhead
i recommend to use Bull insert , also use Dispatcher.Io and try not to block UI while inserting
e
@الخبرات العلمية ScienceExperts I did that already inserting them in chunks on io dispatcher 🙂 thank
🙌 2