Why Paging
The cost of loading everything at once.
Loading Everything Is Expensive
Imagine a feed with 50,000 items. If you fetch and hold them all at once, you waste memory, slow down the network, and freeze the UI while the response is parsed.
Most users only ever scroll through the first few screens. Paging means loading data in small chunks (pages) as the user scrolls, instead of all upfront.
The Problem in Code
A naive approach loads the full list into memory. Even if the server supports it, the response can be huge and the parsed list dominates the heap.
This pattern does not scale and risks OutOfMemoryError on large datasets.
// Anti-pattern: load the whole table at once
suspend fun loadAllArticles(): List<Article> {
// Could be tens of thousands of rows / megabytes of JSON
return api.getArticles(limit = 50_000)
}
// UI holds ALL of them in memory at once
val articles = loadAllArticles()
LazyColumn {
items(articles) { article -> ArticleRow(article) }
}