Slide 1

Slide 1 text

rbusarow COROUTINE-FIRST ANDROID ARCHITECTURE Rick Busarow Photo by Flipboard on Unsplash

Slide 2

Slide 2 text

@rbusarow Coroutines First

Slide 3

Slide 3 text

12 SEPTEMBER 2018 @rbusarow

Slide 4

Slide 4 text

12 SEPTEMBER 2018 @rbusarow

Slide 5

Slide 5 text

http://bit.ly/Structured-Concurrency-Article 12 SEPTEMBER 2018 @rbusarow

Slide 6

Slide 6 text

12 SEPTEMBER 2018 @rbusarow http://bit.ly/Structured-Concurrency-GitHub

Slide 7

Slide 7 text

@rbusarow Structured Concurrency

Slide 8

Slide 8 text

@rbusarow Job hierarchy

Slide 9

Slide 9 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } Pre- Structured Concurrency

Slide 10

Slide 10 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } Pre- Structured Concurrency

Slide 11

Slide 11 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } Pre- Structured Concurrency

Slide 12

Slide 12 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } Pre- Structured Concurrency

Slide 13

Slide 13 text

@rbusarow Job hierarchy Job

Slide 14

Slide 14 text

@rbusarow Job hierarchy Job Job Job Job Job Job Job Job Job Job Job

Slide 15

Slide 15 text

Job @rbusarow Job hierarchy Explicit cancellation Job Job Job Job Job Job Job Job Job Job Job Job Job

Slide 16

Slide 16 text

Job @rbusarow Job hierarchy Explicit cancellation Job Job Job Job Job Job Job Job Job Job .cancel() Job Job Job

Slide 17

Slide 17 text

Job @rbusarow Job hierarchy Explicit cancellation Job Job Job Job Job Job Job Job Job Job Job .cancel()

Slide 18

Slide 18 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } Pre- Structured Concurrency

Slide 19

Slide 19 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } Pre- Structured Concurrency Completely
 Autonomous

Slide 20

Slide 20 text

@rbusarow 0.26 to 1.0

Slide 21

Slide 21 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } 0.26 to 1.0

Slide 22

Slide 22 text

@rbusarow class SomeViewModel : ViewModel() { fun doSomething() { val job = launch { … } } } fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() !-> Unit ): Job { … } 0.26 to 1.0

Slide 23

Slide 23 text

@rbusarow class SomeViewModel : ViewModel(), CoroutineScope { override val coroutineContext = Job() + Dispatchers.Main fun doSomething() { val job = launch { … } } } Structured Concurrency

Slide 24

Slide 24 text

@rbusarow class SomeViewModel : ViewModel(), CoroutineScope { override val coroutineContext = Job() + Dispatchers.Main fun doSomething() { val job = launch { … } } override fun onCleared() { super.onCleared() coroutineContext.cancel() } } Structured Concurrency

Slide 25

Slide 25 text

@rbusarow Lifecycle

Slide 26

Slide 26 text

@rbusarow Androidx Lifecycle

Slide 27

Slide 27 text

@rbusarow Androidx Lifecycle

Slide 28

Slide 28 text

@rbusarow Androidx Lifecycle INITIALIZED STARTED RESUMED CREATED

Slide 29

Slide 29 text

@rbusarow Androidx Lifecycle INITIALIZED STARTED RESUMED CREATED STARTED CREATED DESTROYED

Slide 30

Slide 30 text

@rbusarow Androidx Lifecycle INITIALIZED STARTED RESUMED CREATED STARTED CREATED DESTROYED viewModel init viewModel.onCleared()

Slide 31

Slide 31 text

@rbusarow Androidx Lifecycle STARTED RESUMED CREATED

Slide 32

Slide 32 text

@rbusarow Androidx Lifecycle STARTED RESUMED CREATED onCreate()

Slide 33

Slide 33 text

@rbusarow Androidx Lifecycle STARTED RESUMED CREATED onStart()

Slide 34

Slide 34 text

@rbusarow Androidx Lifecycle STARTED RESUMED CREATED onResume()

Slide 35

Slide 35 text

@rbusarow Androidx Lifecycle STARTED RESUMED CREATED Lifecycle B STARTED RESUMED CREATED Lifecycle A

Slide 36

Slide 36 text

@rbusarow Androidx Lifecycle STARTED RESUMED CREATED Lifecycle C STARTED RESUMED CREATED Lifecycle B STARTED RESUMED CREATED Lifecycle A

Slide 37

Slide 37 text

@rbusarow Androidx Lifecycle STARTED RESUMED CREATED Lifecycle C STARTED RESUMED CREATED Lifecycle B STARTED RESUMED CREATED Lifecycle A …DESTROYED? ¯\_(ツ)_/¯

Slide 38

Slide 38 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main val reminder = launch { while (true) { delay(1_000) Log.v("MainViewModel", "still alive! --> ${UUID.randomUUID()}") } } override fun onCleared() { super.onCleared() cancel() } } ViewModel lifecycle

Slide 39

Slide 39 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main val reminder = launch { while (true) { delay(1_000) Log.v("MainViewModel", "still alive! --> ${UUID.randomUUID()}") } } override fun onCleared() { super.onCleared() cancel() } } ViewModel lifecycle

Slide 40

Slide 40 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main val reminder = launch { while (true) { delay(1_000) Log.v("MainViewModel", "still alive! --> ${UUID.randomUUID()}") } } override fun onCleared() { super.onCleared() cancel() } } ViewModel lifecycle

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main val reminder = launch { while (true) { delay(1_000) Log.v("MainViewModel", "still alive! --> ${UUID.randomUUID()}") } } override fun onCleared() { super.onCleared() cancel() } } ViewModel lifecycle

Slide 44

Slide 44 text

@rbusarow class MainViewModel : ViewModel() { val reminder = viewModelScope.launch { while (true) { delay(1_000) Log.v("MainViewModel", "still alive! --> ${UUID.randomUUID()}") } } } ViewModel lifecycle

Slide 45

Slide 45 text

@rbusarow CoroutineContext

Slide 46

Slide 46 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main } CoroutineContext

Slide 47

Slide 47 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main } CoroutineContext parent Job

Slide 48

Slide 48 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main } CoroutineContext parent Job default Dispatcher

Slide 49

Slide 49 text

@rbusarow class MainViewModel : ViewModel(), CoroutineScope { override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main } CoroutineContext interface CoroutineContext { val job: Job val continuationInterceptor: ContinuationInterceptor val coroutineExceptionHandler: CoroutineExceptionHandler val coroutineName: CoroutineName }

Slide 50

Slide 50 text

@rbusarow fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() !-> Unit ): Job { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyStandaloneCoroutine(newContext, block) else StandaloneCoroutine(newContext, active = true) coroutine.start(start, coroutine, block) return coroutine } CoroutineContext

Slide 51

Slide 51 text

@rbusarow fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() !-> Unit ): Job { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyStandaloneCoroutine(newContext, block) else StandaloneCoroutine(newContext, active = true) coroutine.start(start, coroutine, block) return coroutine } CoroutineContext

Slide 52

Slide 52 text

@rbusarow fun CoroutineScope.newCoroutineContext( context: CoroutineContext ): CoroutineContext { val combined = coroutineContext + context val debug = if (DEBUG) combined + CoroutineId(COROUTINE_ID.incrementAndGet()) else combined return if ( combined !!!== Dispatchers.Default !&& combined[ContinuationInterceptor] !== null ) debug + Dispatchers.Default else debug } CoroutineContext

Slide 53

Slide 53 text

@rbusarow fun CoroutineScope.newCoroutineContext( context: CoroutineContext ): CoroutineContext { val combined = coroutineContext + context val debug = if (DEBUG) combined + CoroutineId(COROUTINE_ID.incrementAndGet()) else combined return if ( combined !!!== Dispatchers.Default !&& combined[ContinuationInterceptor] !== null ) debug + Dispatchers.Default else debug } CoroutineContext

Slide 54

Slide 54 text

@rbusarow public operator fun plus(context: CoroutineContext): CoroutineContext = if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation context.fold(this) { acc, element -> val removed = acc.minusKey(element.key) if (removed === EmptyCoroutineContext) element else { // make sure interceptor is always last in the context (and thus is fast to get when present) val interceptor = removed[ContinuationInterceptor] if (interceptor == null) CombinedContext(removed, element) else { val left = removed.minusKey(ContinuationInterceptor) if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else CombinedContext(CombinedContext(left, element), interceptor) } } } CoroutineContext

Slide 55

Slide 55 text

@rbusarow public operator fun plus(context: CoroutineContext): CoroutineContext = if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation context.fold(this) { acc, element -> val removed = acc.minusKey(element.key) if (removed === EmptyCoroutineContext) element else { // make sure interceptor is always last in the context (and thus is fast to get when present) val interceptor = removed[ContinuationInterceptor] if (interceptor == null) CombinedContext(removed, element) else { val left = removed.minusKey(ContinuationInterceptor) if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else CombinedContext(CombinedContext(left, element), interceptor) } } } CoroutineContext

Slide 56

Slide 56 text

@rbusarow ViewModel lifecycle http://bit.ly/Demystifying-CoroutineContext

Slide 57

Slide 57 text

@rbusarow Dispatchers

Slide 58

Slide 58 text

@rbusarow object Dispatchers { val Default: CoroutineDispatcher val IO: CoroutineDispatcher val Main: CoroutineDispatcher val Unconfined: CoroutineDispatcher } Dispatchers

Slide 59

Slide 59 text

@rbusarow object Dispatchers { val Default: CoroutineDispatcher val IO: CoroutineDispatcher val Main: CoroutineDispatcher val Unconfined: CoroutineDispatcher } Dispatchers CPU-bound

Slide 60

Slide 60 text

@rbusarow object Dispatchers { val Default: CoroutineDispatcher val IO: CoroutineDispatcher val Main: CoroutineDispatcher val Unconfined: CoroutineDispatcher } Dispatchers CPU-bound IO-bound

Slide 61

Slide 61 text

@rbusarow object Dispatchers { val Default: CoroutineDispatcher val IO: CoroutineDispatcher val Main: CoroutineDispatcher val Unconfined: CoroutineDispatcher } Dispatchers CPU-bound IO-bound UI

Slide 62

Slide 62 text

@rbusarow object Dispatchers { val Default: CoroutineDispatcher val IO: CoroutineDispatcher val Main: CoroutineDispatcher val Unconfined: CoroutineDispatcher } Dispatchers CPU-bound IO-bound UI Debugging

Slide 63

Slide 63 text

@rbusarow object Dispatchers { val Default: CoroutineDispatcher val IO: CoroutineDispatcher val Main: CoroutineDispatcher val Unconfined: CoroutineDispatcher } fun doSomething() = launch(Dispatchers.IO) { … } suspend fun doSomethingElse() = withContext(Dispatchers.Main) { … } Dispatchers

Slide 64

Slide 64 text

@rbusarow object Dispatchers { val Default: CoroutineDispatcher val IO: CoroutineDispatcher val Main: CoroutineDispatcher val Unconfined: CoroutineDispatcher } fun doSomething() = launch(Dispatchers.IO) { … } suspend fun doSomethingElse() = withContext(Dispatchers.Main) { … } Dispatchers Test CoroutineDispatcher?

Slide 65

Slide 65 text

@rbusarow TestCoroutineDispatcher

Slide 66

Slide 66 text

@rbusarow advanceTimeBy() advanceUntilIdle() pauseDispatcher() resumeDispatcher() cleanupTestCoroutines() TestCoroutineDispatcher

Slide 67

Slide 67 text

@rbusarow class SomeViewModel : ViewModel(), CoroutineScope { override val coroutineContext = Job() + Dispatchers.Main fun doSomething() { val job = launch { … } } override fun onCleared() { super.onCleared() coroutineContext.cancel() } } CoroutineScope Injection

Slide 68

Slide 68 text

@rbusarow class SomeViewModel(val coroutineScope: CoroutineScope) : ViewModel() { fun doSomething() { coroutineScope.launch { … } } override fun onCleared() { super.onCleared() coroutineScope.cancel() } } CoroutineScope Injection

Slide 69

Slide 69 text

@rbusarow val myScope = object: CoroutineScope { override val coroutineContext = Job() + Dispatchers.Main } val myOtherScope = object: CoroutineScope { override val coroutineContext = Job() + Dispatchers.Default } CoroutineScope Injection

Slide 70

Slide 70 text

@rbusarow class MainCoroutineScope: CoroutineScope { override val coroutineContext = Job() + Dispatchers.Main } class DefaultCoroutineScope: CoroutineScope { override val coroutineContext = Job() + Dispatchers.Default } CoroutineScope Injection

Slide 71

Slide 71 text

@rbusarow class MainCoroutineScopeImpl: MainCoroutineScope { override val coroutineContext = Job() + Dispatchers.Main } class DefaultCoroutineScopeImpl: DefaultCoroutineScope { override val coroutineContext = Job() + Dispatchers.Default } interface MainCoroutineScope: CoroutineScope interface DefaultCoroutineScope: CoroutineScope CoroutineScope Injection

Slide 72

Slide 72 text

@rbusarow class SomeViewModel(val coroutineScope: CoroutineScope) : ViewModel() { fun doSomething() { coroutineScope.launch { … } } override fun onCleared() { super.onCleared() coroutineScope.cancel() } } CoroutineScope Injection

Slide 73

Slide 73 text

@rbusarow class SomeViewModel(val coroutineScope: MainCoroutineScope) : ViewModel() { fun doSomething() { coroutineScope.launch { … } } override fun onCleared() { super.onCleared() coroutineScope.cancel() } } CoroutineScope Injection

Slide 74

Slide 74 text

@rbusarow class MainCoroutineScopeImpl: MainCoroutineScope { override val coroutineContext = Job() + Dispatchers.Main } interface MainCoroutineScope: CoroutineScope class TestMainCoroutineScope: MainCoroutineScope { override val coroutineContext = Job() + TestCoroutineDispatcher() } CoroutineScope Injection

Slide 75

Slide 75 text

@rbusarow fun doSomething() = launch(Dispatchers.IO) { … } suspend fun doSomethingElse() = withContext(Dispatchers.Main) { … } CoroutineScope Injection

Slide 76

Slide 76 text

@rbusarow No hard-coded Dispatchers

Slide 77

Slide 77 text

@rbusarow interface DispatcherProvider { val default: CoroutineDispatcher val io: CoroutineDispatcher val main: CoroutineDispatcher val mainImmediate: CoroutineDispatcher val unconfined: CoroutineDispatcher } DispatcherProvider

Slide 78

Slide 78 text

@rbusarow interface DispatcherProvider : CoroutineContext.Element { override val key: CoroutineContext.Key<*> get() = Key val default: CoroutineDispatcher val io: CoroutineDispatcher val main: CoroutineDispatcher val mainImmediate: CoroutineDispatcher val unconfined: CoroutineDispatcher companion object Key : CoroutineContext.Key } DispatcherProvider

Slide 79

Slide 79 text

@rbusarow class MainCoroutineScope: CoroutineScope { override val coroutineContext = Job() + Dispatchers.Main } DispatcherProvider

Slide 80

Slide 80 text

@rbusarow class MainCoroutineScopeImpl( dispatcherProvider: DispatcherProvider = DefaultDispatcherProvider() ): MainCoroutineScope = MainCoroutineScope { override val coroutineContext = Job() + dispatcherProvider.main + dispatcherProvider } DispatcherProvider

Slide 81

Slide 81 text

@rbusarow class MainCoroutineScopeImpl( dispatcherProvider: DispatcherProvider = DefaultDispatcherProvider() ): MainCoroutineScope = MainCoroutineScope { override val coroutineContext = Job() + dispatcherProvider.main + dispatcherProvider } DispatcherProvider

Slide 82

Slide 82 text

@rbusarow val CoroutineContext.dispatcherProvider: DispatcherProvider get() = get(DispatcherProvider) ?: DefaultDispatcherProvider() DispatcherProvider

Slide 83

Slide 83 text

@rbusarow val CoroutineContext.dispatcherProvider: DispatcherProvider get() = get(DispatcherProvider) ?: DefaultDispatcherProvider() val CoroutineScope.mainDispatcher: CoroutineDispatcher get() = dispatcherProvider.main DispatcherProvider

Slide 84

Slide 84 text

@rbusarow val CoroutineContext.dispatcherProvider: DispatcherProvider get() = get(DispatcherProvider) ?: DefaultDispatcherProvider() val CoroutineScope.mainDispatcher: CoroutineDispatcher get() = dispatcherProvider.main DispatcherProvider

Slide 85

Slide 85 text

@rbusarow class SomeViewModel(val coroutineScope: MainCoroutineScope) : ViewModel() { fun doSomething() { coroutineScope.launch(Dispatchers.Main) { … } } override fun onCleared() { super.onCleared() coroutineScope.cancel() } } CoroutineScope Injection

Slide 86

Slide 86 text

@rbusarow class SomeViewModel(val coroutineScope: MainCoroutineScope) : ViewModel() { fun doSomething() { coroutineScope.launch(coroutineScope.mainDispatcher) { … } } override fun onCleared() { super.onCleared() coroutineScope.cancel() } } CoroutineScope Injection

Slide 87

Slide 87 text

@rbusarow class TestDispatcherProvider( override val default: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val io: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val main: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val mainImmediate: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val unconfined: TestCoroutineDispatcher = TestCoroutineDispatcher() ) : DispatcherProvider TestDispatcherProvider

Slide 88

Slide 88 text

@rbusarow class TestDispatcherProvider( override val default: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val io: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val main: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val mainImmediate: TestCoroutineDispatcher = TestCoroutineDispatcher(), override val unconfined: TestCoroutineDispatcher = TestCoroutineDispatcher() ) : DispatcherProvider fun TestDispatcherProvider(dispatcher: TestCoroutineDispatcher): TestDispatcherProvider = TestDispatcherProvider( default = dispatcher, io = dispatcher, main = dispatcher, mainImmediate = dispatcher, unconfined = dispatcher ) TestDispatcherProvider

Slide 89

Slide 89 text

@rbusarow interface TestProvidedCoroutineScope : TestCoroutineScope, DefaultCoroutineScope, IOCoroutineScope, MainCoroutineScope, MainImmediateCoroutineScope, UnconfinedCoroutineScope { val dispatcherProvider: DispatcherProvider } TestDispatcherProvider

Slide 90

Slide 90 text

@rbusarow class TestProvidedCoroutineScopeImpl( override val dispatcherProvider: DispatcherProvider, context: CoroutineContext = EmptyCoroutineContext ) : TestProvidedCoroutineScope, TestCoroutineScope by TestCoroutineScope(context + dispatcherProvider) TestDispatcherProvider

Slide 91

Slide 91 text

@rbusarow class TestProvidedCoroutineScopeImpl( override val dispatcherProvider: DispatcherProvider, context: CoroutineContext = EmptyCoroutineContext ) : TestProvidedCoroutineScope, TestCoroutineScope by TestCoroutineScope(context + dispatcherProvider) fun TestProvidedCoroutineScope( dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher(), dispatcherProvider: TestDispatcherProvider = TestDispatcherProvider(dispatcher), context: CoroutineContext = EmptyCoroutineContext ): TestProvidedCoroutineScope = TestProvidedCoroutineScopeImpl( dispatcherProvider = dispatcherProvider, context = context + dispatcher ) TestDispatcherProvider

Slide 92

Slide 92 text

@rbusarow class TestProvidedCoroutineScopeImpl( override val dispatcherProvider: DispatcherProvider, context: CoroutineContext = EmptyCoroutineContext ) : TestProvidedCoroutineScope, TestCoroutineScope by TestCoroutineScope(context + dispatcherProvider) fun TestProvidedCoroutineScope( dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher(), dispatcherProvider: TestDispatcherProvider = TestDispatcherProvider(dispatcher), context: CoroutineContext = EmptyCoroutineContext ): TestProvidedCoroutineScope = TestProvidedCoroutineScopeImpl( dispatcherProvider = dispatcherProvider, context = context + dispatcher ) val myScope = TestProvidedCoroutineScope() TestDispatcherProvider

Slide 93

Slide 93 text

@rbusarow class SomeViewModel(val coroutineScope: MainCoroutineScope) : ViewModel() { fun doSomething() { coroutineScope.launch(coroutineScope.mainDispatcher) { … } } override fun onCleared() { super.onCleared() coroutineScope.cancel() } } CoroutineScope Injection

Slide 94

Slide 94 text

@rbusarow class SomeViewModel(val coroutineScope: MainCoroutineScope) : ViewModel() { fun doSomething() { coroutineScope.launch(coroutineScope.mainDispatcher) { … } } override fun onCleared() { super.onCleared() coroutineScope.cancel() } } CoroutineScope Injection

Slide 95

Slide 95 text

@rbusarow class SomeRepository { suspend fun doSomething() = withContext(Dispatchers.IO) { ... } } CoroutineScope Injection

Slide 96

Slide 96 text

@rbusarow class SomeRepository { suspend fun doSomething() = withContext(Dispatchers.IO) { ... } } val CoroutineContext.dispatcherProvider: DispatcherProvider get() = get(DispatcherProvider) ?: DefaultDispatcherProvider() CoroutineScope Injection

Slide 97

Slide 97 text

@rbusarow class SomeRepository { suspend fun doSomething() = withContext(Dispatchers.IO) { ... } } val CoroutineContext.dispatcherProvider: DispatcherProvider get() = get(DispatcherProvider) ?: DefaultDispatcherProvider() suspend fun withIO( context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T ): T { val newContext = coroutineContext.dispatcherProvider.io + context return withContext(newContext, block) } CoroutineScope Injection

Slide 98

Slide 98 text

@rbusarow class SomeRepository { suspend fun doSomething() = withIO { ... } } val CoroutineContext.dispatcherProvider: DispatcherProvider get() = get(DispatcherProvider) ?: DefaultDispatcherProvider() suspend fun withIO( context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T ): T { val newContext = coroutineContext.dispatcherProvider.io + context return withContext(newContext, block) } CoroutineScope Injection

Slide 99

Slide 99 text

@rbusarow github.com/RBusarow/DispatcherProvider

Slide 100

Slide 100 text

rbusarow rbusarow rbusarow THANKS! Rick Busarow [email protected]