Skip to content

Flow android #8

Description

@ChanJun-Park

일반 flow 를 stateFlow 로 만들기

val result: StateFlow<Result<UiState>> = someFlow
  .stateIn(
    initialValue = Result.Loading,
    scope = viewModelScope,
    started = WhileSubscribed(5000),
  )

WhileSubscribed 설명

액티비티가 재실행되는 케이스들

  • rotation change 와 같은 configuration change 발생 -> 어떤 upstream flow 들도 취소되지 않음
  • 사용자가 홈버튼을 눌러서 앱을 background 로 보낸 다음 다시 앱에 진입한 경우 -> 모든 upstream flow 가 timeout 이 지난 뒤에 취소됨

StateFlow 의 collect 가 모두 중지된 경우 upstream flow 도 언제 cancel 시킬건지 결정할 수 있는데, 이를 WhileSubscribed 를 통해 설정해준다. 5초 정도로 설정해서 configuration change 의 경우에는 불필요하게 upstream flow 가 취소되지 않도록 해주고, 홈버튼을 누른 경우처럼 장시간 앱이 background 로 전환될 경우에는 upstream flow 가 취소되도록 해준다.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions