callbacks and coroutines. They are suspending functions, and as all suspending functions - they need to be called from a suspending function, and so on. So where to start?
need a context - it de fi nes the rules for our coroutine We can override any value of the context Job, Dispatchers, CoroutineExceptionHandler, CoroutineName to name a few
Our builders are extension functions to the CoroutineScope. Creating a new coroutine (invoking a builder) inherits the context and creates a parent/child relationship. Each coroutine awaits it’s children before completing
it’s own job! It’s the only context that isn’t inherited - each coroutine creates Its own instance with the inherited as its parent. Creating the hierarchy needed for structured concurrency
it’s own job! It’s the only context that isn’t inherited - each coroutine creates Its own instance with the inherited (or overridden) as its parent. Creating the hierarchy needed for structured concurrency
coroutines. As usual, fl ow collection can be cancelled when the fl ow is suspended in a cancellable suspending function (like delay). Channels Note: Ticker channels are not currently integrated with structured concurrency and their api will change in the future.
- suspendCancellableCoroutine - Can only be called from other suspending functions Scope - They are the logical root of coroutines - Needed for builders - Cancel children, not the scope - Think of viewModelScope when in doubt. Structured concurrency - Understand it! Utilize it! - Even children with di ff erent dispatchers are awaited - Don’t think in callbacks. Job - The controller of structured concurrency - Understand the lifecycle - Be ware of creation to not outplay the structure - One coroutine - One Job Sum up
to re-trow them Exceptions - SupervisorJob / Job di ff erences - Use supervisorScope instead of creating jobs. - Propagates to the root (scope’s handler) - withContext(NonCancellable) Streams - Flows are built on top of coroutines - Channels are used to communicate between coroutines - Channels can’t broadcast - Mind context and use fl owOn - Think about order when collecting fl ows Sum up