Antecipando o início do curso "Desenvolvedor de back-end Kotlin" , convidamos todos a se inscreverem em uma aula aberta sobre o tópico "Multiplataforma Kotlin: frente / verso em um idioma" .
E agora oferecemos a você a tradução tradicional do artigo para leitura.
Hoje temos o prazer de anunciar o lançamento da versão 1.4.0 da biblioteca Kotlin Coroutines. As principais inovações nesta versão são StateFlow e SharedFlow , que agora são APIs estáveis. StateFlow e SharedFlow devem ser usados quando o gerenciamento de estado é necessário no contexto de execução assíncrona usando corrotinas Kotlin.
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 . , .
API, !
Kotlin Coroutines , Kotlin 1.4 Online Event.
"Kotlin Backend Developer".
"Kotlin multiplatform: front/back ".