Files
2nd/10_Wiki/Topic_Programming/Coding/Android_Flow_StateFlow_SharedFlow.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

3.7 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-flow-stateflow-sharedflow Flow / StateFlow / SharedFlow — 어떤 걸 언제 Coding draft B conceptual 2026-05-09 2026-05-09
android
kotlin
flow
vibe-coding
language applicable_to
Kotlin Coroutines
Android
Server
cold flow
hot flow
replay
state vs event

Flow / StateFlow / SharedFlow

셋 다 비동기 stream 이지만 의미 다름: Flow = 1회성 cold, StateFlow = 현재 상태 (hot, 1 replay), SharedFlow = 이벤트 버스 (hot, configurable replay). 잘못 고르면 이벤트 손실 또는 중복 emit.

📖 핵심 개념

  • Cold flow: collect 시작할 때 producer 시작. collector 0 = 비활성. 각 collector 별 독립.
  • Hot flow: producer 가 계속 동작. collector 수와 무관. 다수 collector 공유.
  • StateFlow: 현재 값 보유. 새 collector → 즉시 현재 값 받음. distinctUntilChanged 자동.
  • SharedFlow: 이벤트 list. replay 옵션 (새 collector 가 받을 옛 emission 수).

💻 코드 패턴

Flow — DB / network result

fun getUser(id: String): Flow<User> = flow {
    val u = api.fetch(id)
    emit(u)
}

StateFlow — UI state

class ViewModel : ViewModel() {
    private val _state = MutableStateFlow(UiState.Idle)
    val state: StateFlow<UiState> = _state.asStateFlow()

    fun load() {
        viewModelScope.launch {
            _state.value = UiState.Loading
            // ...
        }
    }
}

SharedFlow — 일회성 이벤트

private val _events = MutableSharedFlow<Event>(replay = 0, extraBufferCapacity = 1)
val events: SharedFlow<Event> = _events.asSharedFlow()

fun showError(msg: String) {
    _events.tryEmit(Event.Error(msg))
}

stateIn — Flow → StateFlow 변환

val state: StateFlow<UiState> = repo.observeUser(id)
    .map { UiState.Success(it) }
    .stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000), // 5s 후 unsubscribe
        initialValue = UiState.Loading,
    )

combine, flatMapLatest, debounce

val results = combine(query.debounce(300), filters) { q, f -> q to f }
    .flatMapLatest { (q, f) -> repo.search(q, f) }
    .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())

🤔 의사결정 기준

데이터 권장
1회성 fetch (사용자 정보, network call) Flow
UI 가 항상 봐야 할 현재 state StateFlow
Snackbar / Navigation 같은 일회성 이벤트 SharedFlow (replay=0)
다수 구독자 + buffered SharedFlow + replay/buffer
Database (Room flow) Flow → stateIn
화면 회전 시 이벤트 재실행 막기 SharedFlow + STARTED 만 collect

안티패턴

  • 이벤트 (Snackbar) 를 StateFlow 로: state 라 회전 시 재실행. SharedFlow + replay=0.
  • state 를 SharedFlow 로: 새 collector 가 옛 값 못 받음 (replay=0). StateFlow 가 맞음.
  • stateIn 없이 cold flow 를 UI 에서 collect: 회전 시 새 collect = 새 fetch. stateIn(WhileSubscribed) 권장.
  • collectLatest 대신 collect (검색 같은 케이스): 이전 결과가 늦게 도착해 새 결과 덮어씀. flatMapLatest / collectLatest.
  • MutableStateFlow 를 외부 노출: 외부 mutate 가능. asStateFlow().
  • emit 대신 tryEmit 인데 buffer 0: drop. 버퍼 또는 emit (suspend).

🤖 LLM 활용 힌트

  • "현재 상태 = StateFlow, 일회성 이벤트 = SharedFlow(replay=0), Room/network = Flow + stateIn" 명시.
  • WhileSubscribed(5000) 이 표준.

🔗 관련 문서