Coroutines Update Seva Tolstopyatov @qwwdfsad October 13, 2020 - - PowerPoint PPT Presentation

coroutines update seva tolstopyatov
SMART_READER_LITE
LIVE PREVIEW

Coroutines Update Seva Tolstopyatov @qwwdfsad October 13, 2020 - - PowerPoint PPT Presentation

Kotlin 1.4 Online Event Coroutines Update Seva Tolstopyatov @qwwdfsad October 13, 2020 Coroutines debugging Coroutines debugging Coroutines debugging Coroutines debugging Coroutines debugging IDEA 2020.1 Coroutines debugging Debugging


slide-1
SLIDE 1

October 13, 2020 @qwwdfsad Kotlin 1.4 Online Event

Coroutines Update Seva Tolstopyatov

slide-2
SLIDE 2

Coroutines debugging

slide-3
SLIDE 3

Coroutines debugging

slide-4
SLIDE 4

Coroutines debugging

slide-5
SLIDE 5

Coroutines debugging

slide-6
SLIDE 6

Coroutines debugging – IDEA 2020.1

slide-7
SLIDE 7

Coroutines debugging

slide-8
SLIDE 8

Debugging meets coroutines 1.3.8

slide-9
SLIDE 9

Flow since 1.3

slide-10
SLIDE 10

Flow

val flow: Flow<Int> = flow { delay(100) for (i in 1..10) { emit(i) } }.map { delay(100) it * it }

slide-11
SLIDE 11

StateFlow

A

B

E D F C

slide-12
SLIDE 12

var variable: Int = 42

State A condition or way of being that exists at a particular time

slide-13
SLIDE 13

StateFlow

slide-14
SLIDE 14

StateFlow

slide-15
SLIDE 15

StateFlow

slide-16
SLIDE 16

StateFlow

public interface StateFlow<out T> : Flow<T> { public val value: T }

slide-17
SLIDE 17

StateFlow

public interface MutableStateFlow<T> : Flow<T> { public override var value: T }

slide-18
SLIDE 18

StateFlow

class DownloadingModel { private val _status = MutableStateFlow<DownloadStatus>(DownloadStatus.NOT_REQUESTED) val status: StateFlow<DownloadStatus> get() = _status suspend fun download() { } }

slide-19
SLIDE 19

StateFlow

class DownloadingModel { private val _status = MutableStateFlow<DownloadStatus>(DownloadStatus.NOT_REQUESTED) val status: StateFlow<DownloadStatus> get() = _status suspend fun download() { _status.value = DownloadStatus.INITIALIZED initializeConnection() } }

slide-20
SLIDE 20

StateFlow

class DownloadingModel { private val _status = MutableStateFlow<DownloadStatus>(DownloadStatus.NOT_REQUESTED) val status: StateFlow<DownloadStatus> get() = _status suspend fun download() { _status.value = DownloadStatus.INITIALIZED initializeConnection() processAvailableContent { partialData: ByteArray, downloadedBytes: Long, totalBytes: Long -> storePartialData(partialData) _status.value = DownloadProgress(downloadedBytes.toDouble() / totalBytes) } } }

slide-21
SLIDE 21

StateFlow

class DownloadingModel { private val _status = MutableStateFlow<DownloadStatus>(DownloadStatus.NOT_REQUESTED) val status: StateFlow<DownloadStatus> get() = _status suspend fun download() { _status.value = DownloadStatus.INITIALIZED initializeConnection() processAvailableContent { partialData: ByteArray, downloadedBytes: Long, totalBytes: Long -> storePartialData(partialData) _status.value = DownloadProgress(downloadedBytes.toDouble() / totalBytes) } _status.value = DownloadStatus.SUCCESS } }

slide-22
SLIDE 22

SharedFlow

Single-event processing Multiple-event processing e 1 e2 e3 e4 e5 e6

t

e 1 e 13

slide-23
SLIDE 23

SharedFlow

slide-24
SLIDE 24

SharedFlow

  • Costly connections
  • May be unused
  • Replay log
  • Flexibility
slide-25
SLIDE 25

Existing solutions

  • Subjects: BehaviorSubject, AsyncSubject, ReplaySubject
  • ConnectableFlowable: connect, refCount, autoConnect
  • Processors: Emitter, Unicast
  • Share, publish, replay
slide-26
SLIDE 26

SharedFlow

interface SharedFlow<out T> : Flow<T> { public val replayCache: List<T> }

slide-27
SLIDE 27

SharedFlow

interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> { suspend fun emit(value: T) fun tryEmit(value: T): Boolean val subscriptionCount: StateFlow<Int> fun resetReplayCache() }

slide-28
SLIDE 28

SharedFlow

public fun <T> MutableSharedFlow( replay: Int, extraBufferCapacity: Int = 0,

  • nBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND

): MutableSharedFlow<T>

slide-29
SLIDE 29

SharedFlow

public fun <T> Flow<T>.shareIn( scope: CoroutineScope, replay: Int, started: SharingStarted = SharingStarted.Eagerly )

slide-30
SLIDE 30

Flow since 1.3

  • Core operators

○ catch, onEmpty, onCompletion, onStart ○

  • nEach, transform, transformWhile
  • Invariants
slide-31
SLIDE 31

Flow since 1.3

suspend fun Flow<Int>.stopOn42() = collect { println(it) if (it == 42) { throw AnswerFoundException() } }

slide-32
SLIDE 32

Flow since 1.3

flow { try { emit(42) } catch (e: AnswerFoundException) { emit(21) } }.stopOn42()

slide-33
SLIDE 33

Flow since 1.3

java.lang.IllegalStateException: Flow exception transparency is violated: Previous 'emit' call has thrown exception java.util.concurrent.CancellationException: Thanks, I had enough of your data, but then emission attempt of value '21' has been detected. Emissions from 'catch' blocks are prohibited in order to avoid unspecified behaviour, 'Flow.catch' operator can be used instead. For a more detailed explanation, please refer to Flow documentation. at kotlinx.coroutines.flow.internal.SafeCollector.exceptionTransparencyViolated(Saf eCollector.kt:114

slide-34
SLIDE 34

Flow since 1.3

flowOf(42) .catch { e -> println("Answer was found") } .stopOn42()

slide-35
SLIDE 35

Android update

slide-36
SLIDE 36

1 github.com/Kotlin/kotlinx.coroutines/pull/1652

Android update

  • The coroutines DEX size is optimized by 30%
  • CPU consumption of default dispatchers was drastically reduced
  • Startup time was significantly optimised
slide-37
SLIDE 37

JDK update

slide-38
SLIDE 38

JDK update – Blocking calls

withTimeout(500.milliseconds) { runInterruptible(Dispatchers.IO) { serverSocket.accept() } }

slide-39
SLIDE 39

More JDK updates

  • Out-of-the-box integration with BlockHound
  • Integration with JDK 9 java.util.concurrent.Flow

1 github.com/reactor/BlockHound 2 docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html

slide-40
SLIDE 40

The future of coroutines

1 github.com/Kotlin/kotlinx.coroutines/pull/1937

  • SharedFlow and StateFlow stabilization
  • Concise and cancellation-aware resource management
  • Replacement for offer and poll
  • More Flow time API for UI programming
  • kotlinx-coroutines-test stabilization
  • Sliceable dispatchers
slide-41
SLIDE 41

@qwwdfsad

Thanks! Have a nice Kotlin!