Paging 3 Flashcards
ARCHITECTURE › Components
- What are the three return types of PagingSource.load(), and what does each mean?
- LoadResult.Page (data plus prevKey/nextKey; a null key means no more pages in that direction), LoadResult.Error (load failed, surfaced as LoadState.Error), and LoadResult.Invalid (the source is stale, so Paging discards it and creates a new PagingSource).
- What is getRefreshKey() for, and why is it needed?
- It returns the key Paging uses to reload after invalidation or refresh so the user stays near their current scroll position. It is computed from PagingState.anchorPosition and the closest loaded page's prevKey/nextKey; returning null restarts from the initial key.
- Why must you call cachedIn() on a Flow<PagingData>, and what scope do you pass?
- cachedIn() makes the flow shareable and caches loaded pages in a CoroutineScope (usually viewModelScope) so data survives configuration changes and multiple/repeated collectors do not trigger fresh loads. It is also required before multiplecasting or collecting a PagingData flow more than once.
- Explain the key PagingConfig parameters: pageSize, prefetchDistance, initialLoadSize, enablePlaceholders.
- pageSize is items per page; prefetchDistance is how close to the loaded edge the user must scroll to trigger the next load (default pageSize); initialLoadSize is the first load amount (default pageSize * 3); enablePlaceholders shows null placeholders for not-yet-loaded items, requiring the source to report total count.
- When do you use a RemoteMediator, and what is the single source of truth?
- Use a RemoteMediator when paging from a layered source: network fetches feed a local database, and the PagingSource reads from the DB. The database is the single source of truth, giving offline support; the mediator's load() fetches network pages and writes them to the DB.
- What are the LoadType values passed to RemoteMediator.load(), and what does MediatorResult.Success(endOfPaginationReached) signal?
- LoadType is REFRESH, PREPEND, or APPEND. MediatorResult.Success(endOfPaginationReached=true) tells Paging there is no more data in that direction so it stops requesting more; false means more can be loaded. MediatorResult.Error reports failure.
- What is CombinedLoadStates and how do refresh, append, and prepend differ?
- CombinedLoadStates groups LoadState (Loading, NotLoading, Error) for each load direction. refresh is the initial or refreshed load (drive a full-screen spinner/empty/error), append loads at the list end, and prepend loads at the start (drive inline footer/header indicators). It also exposes separate source and mediator states.
- In Compose, how do you render a Pager and key its items correctly?
- Collect with pager.flow.collectAsLazyPagingItems(), then in LazyColumn use items(count = items.itemCount, key = items.itemKey { it.id }, contentType = items.itemContentType { ... }) and access items[index], which may be null when placeholders are enabled. Drive spinners/retry from items.loadState.
- How do you add separators, headers, or footers, and where in the chain does it go?
- Map the PagingData and call insertSeparators { before, after -> ... } (return a separator item or null), or insertHeaderItem/insertFooterItem. Item types must be a common/sealed type, and it is applied before cachedIn so the result is cached too.