Files
2nd/10_Wiki/Topic_Programming/Coding/Android_Paging_3_Patterns.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

4.6 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
android-paging-3-patterns Android Paging 3 — 효율적 페이지네이션 Coding draft B conceptual 2026-05-09 2026-05-09
android
paging
list
vibe-coding
language applicable_to
Kotlin / Jetpack Paging 3
Android
PagingSource
RemoteMediator
PagingData
LazyColumn

Android Paging 3

큰 list 를 chunk 로 fetch + 캐시 + 무한 스크롤. PagingSource (단일 source) 또는 RemoteMediator + Room (network + DB) 두 패턴. Compose / RecyclerView 모두 지원.

📖 핵심 개념

  • PagingSource: load(params) → PagingSourceLoadResult.
  • Pager: configuration (pageSize, prefetch).
  • PagingData: ViewModel 에서 Compose / Adapter 로 흐름.

💻 코드 패턴

단일 PagingSource (network only)

class UserPagingSource(private val api: UserApi) : PagingSource<Int, User>() {
    override fun getRefreshKey(state: PagingState<Int, User>): Int? {
        return state.anchorPosition?.let { state.closestPageToPosition(it)?.prevKey?.plus(1) }
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, User> = try {
        val page = params.key ?: 1
        val res = api.fetchUsers(page = page, size = params.loadSize)
        LoadResult.Page(
            data = res.items,
            prevKey = if (page == 1) null else page - 1,
            nextKey = if (res.items.isEmpty()) null else page + 1,
        )
    } catch (e: IOException) {
        LoadResult.Error(e)
    }
}

class UserViewModel(api: UserApi) : ViewModel() {
    val users: Flow<PagingData<User>> = Pager(
        config = PagingConfig(pageSize = 20, prefetchDistance = 3),
        pagingSourceFactory = { UserPagingSource(api) }
    ).flow.cachedIn(viewModelScope)
}

Compose 사용

@Composable
fun UserList(viewModel: UserViewModel) {
    val users = viewModel.users.collectAsLazyPagingItems()

    LazyColumn {
        items(users.itemCount, key = users.itemKey { it.id }) { index ->
            users[index]?.let { UserRow(it) }
        }

        when (val s = users.loadState.append) {
            is LoadState.Loading -> item { Spinner() }
            is LoadState.Error -> item { ErrorRow(s.error) { users.retry() } }
            else -> Unit
        }
    }
}

RemoteMediator (network + DB cache)

@OptIn(ExperimentalPagingApi::class)
class UserRemoteMediator(
    private val api: UserApi, private val db: AppDb
) : RemoteMediator<Int, UserEntity>() {

    override suspend fun load(loadType: LoadType, state: PagingState<Int, UserEntity>): MediatorResult {
        try {
            val page = when (loadType) {
                LoadType.REFRESH -> 1
                LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)
                LoadType.APPEND -> {
                    val last = state.lastItemOrNull() ?: return MediatorResult.Success(true)
                    last.page + 1
                }
            }
            val res = api.fetchUsers(page = page, size = state.config.pageSize)
            db.withTransaction {
                if (loadType == LoadType.REFRESH) db.userDao().clear()
                db.userDao().upsertAll(res.items.map { it.toEntity(page) })
            }
            return MediatorResult.Success(endOfPaginationReached = res.items.isEmpty())
        } catch (e: Exception) {
            return MediatorResult.Error(e)
        }
    }
}

// ViewModel
val users = Pager(
    config = PagingConfig(pageSize = 20),
    remoteMediator = UserRemoteMediator(api, db),
    pagingSourceFactory = { db.userDao().pagingSource() }
).flow.cachedIn(viewModelScope)

🤔 의사결정 기준

상황 패턴
Network only, 캐시 불필요 PagingSource
Network + DB 캐시 / 오프라인 RemoteMediator + Room
검색 (key 가 string) PagingSource<String, ...>
Cursor pagination key = cursor
작은 list (50개 미만) Paging 불필요 — 그냥 Flow

안티패턴

  • cachedIn 없이: configuration change 마다 새 fetch. cachedIn(viewModelScope).
  • getRefreshKey 잘못: refresh 시 첫 page 로 다시 → 사용자 위치 잃음.
  • key 로 unstable id (timestamp): 같은 row 가 다른 page 에 나타남.
  • error 상태 무시: 사용자 멈춤 모름. retry button.
  • endOfPaginationReached 잘못 판정: 무한 fetch 또는 일찍 멈춤.

🤖 LLM 활용 힌트

  • 신규 = Paging 3 + Compose collectAsLazyPagingItems.
  • offline 필요 = RemoteMediator.

🔗 관련 문서