Issue
Say, I am going to download two tables of data from the web server, categories
and items
:
data class Category(val id: Long)
data class Item(val id: Long, val name: String, val categoryId: Long)
Then populate the data into ViewPager2
and TabLayout
like below:
I have two strategies of doing this:
Strategy 1: Request all the data at once, reorganize the data by code (filtering, sorting...), then pass corresponding data (items
) to each page Fragment
.
Strategy 2: Request corresponding data for each page (by categoryId
), which means the web server will do the filtering and sorting and each Fragment
requests its data individualy.
Which strategy is better? Thanks in advance.
Solution
Better is subjective - it very much depends on the data you're going to fetch, and the metrics which are important to your users.
If you have a small amount of data then fetch it all in one api call and split on the device - that's far simpler to think about, and far simpler to code. It's probably going to be faster to write this as well, so if you want to release something quickly this is probably the best approach.
However, if you try fetching it all at once but there is a large amount of data then your users are going to be spending a lot of time watching a loading spinner! In that case, I'd fetch the data when each list is asked for by the user.
If you design your web server's api nicely, you can make the same api endpoint solve both approaches - i.e. the filter/sort parameters can be optional. This would let you build and release the single-call approach (by passing no parameters to the api endpoint and just getting all the data) and then, if that isn't good enough you can release another version of your app which does the filtering server-side.
Answered By - deanWombourne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.