Skip to content

Flow: Lifecycle operators

Devrath edited this page Dec 27, 2023 · 1 revision

What are lifecycle operators

These are the operators that are triggered only once during the lifecycle of a flow

Some lifecycle operators

  • onStart -------> Triggered during the start of the flow
  • onCompletion --> Triggered during the completion of flow

Code

        viewModelScope.launch {
            data.getData(context)
                .map { dataList ->
                    
                }.onStart {
                    // We indicate the UI will start loading
                }.catch { error ->
                    // Emit the error if the error is found
                }.onCompletion {
                    // This indicates flow is completed
                }.collect()
        }
Clone this wiki locally