What we need
load {
loadWeather(“Bengaluru”)
} thenOnUI {
showWeather(it)
}
Slide 8
Slide 8 text
Corountines
It can be thought of as an instance of suspend-able
computation
non-blocking code
Coroutines simplify asynchronous programming
by providing possibility to write code in
direct style (sequentially).
Slide 9
Slide 9 text
“Truth can only be
found in one place:
the code”
Slide 10
Slide 10 text
Coroutine Example
launch {
println("CR start : ${getThreadName()}")
Thread.sleep(100)
println("CR ended : ${getThreadName()}")
}
run {
println("Run start: ${getThreadName()}")
Thread.sleep(300)
println("Run ended: ${getThreadName()}")
}
fun funExperiment() {
}
Slide 11
Slide 11 text
Run start: main
CR start : commonPool-worker-1
Output
Coroutine Example
launch {
println("CR start : ${getThreadName()}")
Thread.sleep(100)
println("CR ended : ${getThreadName()}")
}
run {
println("Run start: ${getThreadName()}")
Thread.sleep(300)
println("Run ended: ${getThreadName()}")
}
fun funExperiment() {
}
Slide 12
Slide 12 text
CR ended : commonPool-worker-1
Run ended: main
Output
Coroutine Example
launch {
println("CR start : ${getThreadName()}")
Thread.sleep(100)
println("CR ended : ${getThreadName()}")
}
run {
println("Run start: ${getThreadName()}")
Thread.sleep(300)
println("Run ended: ${getThreadName()}")
}
}
Run start: main
CR start : commonPool-worker-1
fun funExperiment() {
Slide 13
Slide 13 text
Blocking
Thread
Time
Function A Blocked
Blocked Function B
Slide 14
Slide 14 text
Suspended fun
Thread
Time
Function A
Suspended
Function B
val uiContext: CoroutineContext = UI
val bgContext: CoroutineContext = CommonPool
Context
Slide 30
Slide 30 text
The plan
1. start coroutine
2. var apiData = fetchDataFromServer().await()
3. var result = parseData(apiData).await()
4. displayInList(data)
5. DSL
Slide 31
Slide 31 text
launch(context = bgContext) {
val weatherResult = loadWeather("Bengaluru")
}
API
Slide 32
Slide 32 text
Coroutine context
API
launch(context = bgContext) {
val weatherResult = loadWeather("Bengaluru")
}
Slide 33
Slide 33 text
Coroutine builder
launch(context = bgContext) {
val weatherResult = loadWeather("Bengaluru")
}
Coroutine context
API
Slide 34
Slide 34 text
val job = launch(context = CommonPool) {
val weatherResult = loadWeather("Bengaluru")
launch(context = UI) {
showWeather(weatherResult)
}
}
API and then show results
Slide 35
Slide 35 text
Another Coroutine
API and then show results
val job = launch(context = CommonPool) {
val weatherResult = loadWeather("Bengaluru")
launch(context = UI) {
showWeather(weatherResult)
}
}
Slide 36
Slide 36 text
job.cancel() //cancels the execution of parent and child coroutines
val job = launch(context = CommonPool) {
val weatherResult = loadWeather("Bengaluru")
launch(context = UI) {
showWeather(weatherResult)
}
}
API and then show results
Slide 37
Slide 37 text
• Simpler thread switching
• Simple DSL
• Dancing with final code
Coroutines
The Fun Solution
Slide 38
Slide 38 text
Solution
fun getWeather(city="Bengaluru") {
async(context = UI) {
view.showLoading()
val result = withContext(bgContext) {
loadWeather(city)
}
showWeather(result)
}
}
Slide 39
Slide 39 text
Solution
fun getWeather(city="Bengaluru") {
launch(context = UI) {
view.showLoading()
val result = withContext(bgContext) {
loadWeather(city)
}
showWeather(result)
}
}
Slide 40
Slide 40 text
Solution
fun getWeather(city="Bengaluru") {
launch(context = UI) {
view.showLoading()
val result = withContext(bgContext) {
loadWeather(city)
}
showWeather(result)
}
}
Slide 41
Slide 41 text
Solution
fun getWeather(city="Bengaluru") {
launch(context = UI) {
view.showLoading()
val result = withContext(bgContext) {
loadWeather(city)
}
showWeather(result)
}
}
Slide 42
Slide 42 text
suspend fun loadWeather (city="Bengaluru") {
dataRepository.loadweather(city)
}
fun showWeather(weatherResult: List) {
view.showWeather(weatherResult)
}
Solution
Slide 43
Slide 43 text
Where we were
@WorkerThread
fun loadWeather(city: String): List {
…
}
@MainThread
fun showWeather(weather: List) {
…
}
Slide 44
Slide 44 text
Solution
fun getWeather(city="Bengaluru") {
launch(context = UI) {
view.showLoading()
val result = withContext(bgContext) {
loadWeather(city)
}
showWeather(result)
}
}
Slide 45
Slide 45 text
Fun DSL’s
suspend fun load(block: () -> T): Deferred {
}
return block()
fun Deferred.thenOnUI(uiFunction: (T) -> Unit) {
}
launch(UI) { uiFunction([email protected]()) }