Anticipándonos al inicio del curso " Kotlin Backend Developer" , invitamos a todos a inscribirse en una lección abierta sobre el tema "Kotlin multiplataforma: anverso / reverso en un idioma" .
Y ahora te ofrecemos la traducción tradicional del artículo para su lectura.
Hoy nos complace anunciar el lanzamiento de la versión 1.4.0 de la biblioteca Kotlin Coroutines. Las principales innovaciones de esta versión son StateFlow y SharedFlow , que ahora son API estables. StateFlow y SharedFlow están pensados para su uso cuando se requiere la gestión del estado en el contexto de la ejecución asíncrona mediante Kotlin Coroutines.
API- Flow Kotlin , . , Flow — . Kotlin Flow , Sequences: , , . . Sequences Flow Kotlin , Flow .
Flow : , API- Flow. Flow backpressure, — .
val flow: Flow<Int> = flow {
delay(100)
for(i in 1..10) {
emit(i)
}
}.map {
delay(100)
it * it
}
Flow , Sequences. Flow , backpressure.
Flow API-, , . , . : , «» «», «» «». : .
API- Flow , . ConflatedBroadcastChannel. ConflatedBroadcastChannel . , , . , . , - !
ConflatedBroadcastChannel API- — StateFlow SharedFlow!
StateFlow
StateFlow : StateFlow MutableStateFlow:
public interface StateFlow<out T> : SharedFlow<T> {
public val value: T
}
public interface MutableStateFlow<out T>: StateFlow<T>, MutableSharedFlow<T> {
public override var value: T
public fun compareAndSet(expect: T, update: T): Boolean
}
. .
, API-.
class DownloadingModel {
private val _state.value = MutableStateFlow<DownloadStatus>(DownloadStatus.NOT_REQUESTED)
val state: StateFlow<DownloadStatus> get() = _state
suspend fun download() {
_state.value = DownloadStatus.INITIALIZED
initializeConnection()
processAvailableContent {
partialData: ByteArray,
downloadedBytes: Long,
totalBytes: Long
->
storePartialData(partialData)
_state = DownloadProgress(downloadedBytes.toDouble() / totalBytes)
}
_state.value = DownloadStatus.SUCCESS
}
}
state, (state) . : state.value = DownloadStatus.INITIALIZED
. , , . , state
, .
, API . , - . , , state
Flow.
SharedFlow
, , ? API- SharedFlow. API- , .
public interface SharedFlow<out T> : Flow<T> {
public val replayCache: List<T>
}
— , , . , .
SharedFlow MutableSharedFlow.
interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> {
suspend fun emit(value: T)
fun tryEmit(value: T): Boolean
val subscriptionCount: StateFlow<Int>
fun resetReplayCache()
}
MutableSharedFlow . , MutableSharedFlow . , .
MutableSharedFlow . SharedFlow.
public fun <T> MutableSharedFlow(
replay: Int,
extraBufferCapacity: Int = 0,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND
): MutableSharedFlow<T>
MutableSharedFlow , , , , , . , , .
, Flow.shareIn
.
public fun <T> Flow<T>.shareIn(
scope: CoroutineScope,
replay: Int,
started: SharingStarted = SharingStarted.Eagerly
)
API- StateFlow SharedFlow Kotlin . , .
Kotlin Coroutines , Kotlin 1.4 Online Event.
"Kotlin Backend Developer".
"Kotlin multiplatform: front/back ".