Documentos de Académico
Documentos de Profesional
Documentos de Cultura
FLOWS
REPOSITORY
REPOSITORY
REPOSITORY
- Constructed on top of
coroutines
- Reactive programming
"In coroutines, a flow is a type that can emit multiple values sequentially, as
opposed to suspend functions that return only a single value."
COLD FLOWS
Los flows son lazy, creados
on-demand cuando
comenzas a colectarlos.
COLLECT FLOWS - LiveData
- Flows tiene una integración con LiveData, por lo que al llegar al ViewModel uno puede
convertir el flow a live data y usarlo como ya sabemos.
COLLECT FLOWS - collect
- La idea no está muy alejada de como observamos live data. A los flows les hacemos
collect. La forma para esto es:
- Launch en un scope
- Hay que usar
repeatOnLifecycle
- Lifecycle.State.STARTED
- Para menos verborragia en
cada collect se puede armar
alguna extension y/o dentro
de repeatOnLifecycle poner
varios launch
Preguntas hasta ahora?
StateFlow
- "State-holder observable flow that emits the current and new state
to its collectors".
StateFlow docs
Migrating from LiveData to Kotlins Flow
-> Configuring the exposed StateFlow (stateIn operator)
.asLiveData()
Transformaciones
Podemos realizar varias
transformaciones en el medio,
recomiendo ver los kotlin docs combine(flow1, flow2, …) {...}
para el flow package
.catch {...}
.map {...}
.filter {...}
.map {...}
flowOn
- Ejecutar en distintos CoroutineContexts .IO
- Cambia el contexto al upstream flow (above)
.flowOn(Dispatchers.IO)
.Default
.flowOn(Dispatchers.Default)
.Main
SharedFlow
- También es un hot flow.
SharedFlow docs
Reemplazo para SingleLiveEvent