Intro

This post shows steps and code comparison of migrating todo-mvp-kotlin sample from callbacks to kotlin coroutines.

Work is currently completed in my fork: mvp-kotlin-coroutines. This is external sample, but link is available in official google repository.

What you need

Before exploring this sample, you should familiarize yourself with the following topics:

What we want to achieve?

Replace all asynchronous operations with coroutines, which simplify asynchronous programming by providing possibility to write code in direct style (sequentially).

Dependencies

First, we need to introduce two more dependencies.

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core: $version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android: $version"

To ignore the warning that coroutines are experimental, mark them as enabled.

kotlin {

experimental {

coroutines "enable"

}

}

Commit reference: 32b0b1f

Base Implementation

All asynchronous operations are done via TasksRepository , TasksLocalDataSource , and TasksRemoteDataSource which implement TasksDataSource interface.