KOTLIN
COROUTINES
in Android
2018 / KOTLIN COROUTINES IN ANDROID
COROUTINES
Async
easy
Coroutines provide a way to
avoid blocking a thread and
simplify asynchronous
programming
SLIDE HEADLINE / Kotlin Coroutines in Android
COROUTINES
SLIDE HEADLINE / Kotlin Coroutines in Android
Blocking vs
Suspending
Coroutines are computations
that can be suspended without
blocking a thread
Suspending
2018 / KOTLIN COROUTINES IN ANDROID
COROUTINES
SLIDE HEADLINE / Kotlin Coroutines in Android
Launch
launch { … } is a Kotlin regular
function, which takes
suspending block and returns a
Job instance
Suspending
Launch
2018 / KOTLIN COROUTINES IN ANDROID
COROUTINES
SLIDE HEADLINE / Kotlin Coroutines in Android
Async
Await
async { … } in Kotlin is regular
function with suspending
parameter
await() is a method to get value
from suspending function done
asynchronous
Suspending
Launch
Async / await
2018 / KOTLIN COROUTINES IN ANDROID
Network
request
We want to send a request to
get some information …
fun getLocation(): Location {
/// gets current location
return location
}
2018 / KOTLIN COROUTINES IN ANDROID
Network
request
… and some more information
based on previous ...
fun getLocation(): Location { … }
fun getWeather(location: Location): Weather {
/// gets weather for current location
return weather
}
2018 / KOTLIN COROUTINES IN ANDROID
Network
request
… and show the result
fun getLocation(): Location { … }
fun getWeather(location: Location): Weather { … }
fun showWeather(weather: Weather) {
/// shows weather on the screen
}
2018 / KOTLIN COROUTINES IN ANDROID
fun getLocationAsync(cb: (Location) -> Unit) {
/// gets current location and returns it
/// with callback when done
}
Callbacks
We can do same with callbacks
2018 / KOTLIN COROUTINES IN ANDROID
fun getLocationAsync(cb: (Location) -> Unit) { … }
fun getWeatherAsync(location: Location,
cb: (Weather) -> Unit) {
/// gets weather for current location async
}
Callbacks
We can do same with callbacks
2018 / KOTLIN COROUTINES IN ANDROID
fun getLocationAsync(cb: (Location) -> Unit) { … }
fun getWeatherAsync(location: Location,
cb: (Weather) -> Unit) { … }
fun showWeather(weather: Weather) {
/// shows weather on the screen
}
Callbacks
We can do same with callbacks
2018 / KOTLIN COROUTINES IN ANDROID
fun getLocationAsync(cb: (Location) -> Unit) { … }
fun getWeatherAsync(location: Location,
cb: (Weather) -> Unit) { … }
fun showWeather(weather: Weather) { … }
fun processWeather() {
getLocationAsync { location ->
getWeatherAsync { weather ->
showWeather(weather)
}
}
}
Callbacks
We can do same with callbacks
2018 / KOTLIN COROUTINES IN ANDROID
Suspending
function
Suspending function suspends
the coroutine, without blocking
the thread
suspend fun getLocation(): Location {
/// gets current location & suspends
return location
}
2018 / KOTLIN COROUTINES IN ANDROID
Suspending
function
Suspending function suspends
the coroutine, without blocking
the thread
suspend fun getLocation(): Location { … }
suspend fun getWeather(location: Location): Weather {
/// gets weather for current location &
/// suspends
return weather
}
2018 / KOTLIN COROUTINES IN ANDROID
Suspending
function
Suspending function suspends
the coroutine, without blocking
the thread
2018 / KOTLIN COROUTINES IN ANDROID
suspend fun getLocation(): Location { … }
suspend fun getWeather(location: Location)
: Weather { … }
fun showWeather(weather: Weather) {
/// shows weather on the screen
}
suspend fun processWeather() {
val location = getLocation()
val weather = getWeather(location)
showWeather(weather)
}
2018 / KOTLIN COROUTINES IN ANDROID
Launch
coroutine
/** Simple countdown with coroutines */
fun countdown(start: Int) {
launch(UI) {
for (i in start downTo 0) {
/// show time
delay(1000)
}
}
}
2018 / KOTLIN COROUTINES IN ANDROID
Place your screenshot
here
2018 / KOTLIN COROUTINES IN ANDROID
Thanks!
Any questions?
You can find me at
me@robertlevonyan.com
Or tweet
@robertlevonyan

Kotlin coroutines

  • 1.
  • 2.
    2018 / KOTLINCOROUTINES IN ANDROID COROUTINES Async easy Coroutines provide a way to avoid blocking a thread and simplify asynchronous programming SLIDE HEADLINE / Kotlin Coroutines in Android
  • 3.
    COROUTINES SLIDE HEADLINE /Kotlin Coroutines in Android Blocking vs Suspending Coroutines are computations that can be suspended without blocking a thread Suspending 2018 / KOTLIN COROUTINES IN ANDROID
  • 4.
    COROUTINES SLIDE HEADLINE /Kotlin Coroutines in Android Launch launch { … } is a Kotlin regular function, which takes suspending block and returns a Job instance Suspending Launch 2018 / KOTLIN COROUTINES IN ANDROID
  • 5.
    COROUTINES SLIDE HEADLINE /Kotlin Coroutines in Android Async Await async { … } in Kotlin is regular function with suspending parameter await() is a method to get value from suspending function done asynchronous Suspending Launch Async / await 2018 / KOTLIN COROUTINES IN ANDROID
  • 6.
    Network request We want tosend a request to get some information … fun getLocation(): Location { /// gets current location return location } 2018 / KOTLIN COROUTINES IN ANDROID
  • 7.
    Network request … and somemore information based on previous ... fun getLocation(): Location { … } fun getWeather(location: Location): Weather { /// gets weather for current location return weather } 2018 / KOTLIN COROUTINES IN ANDROID
  • 8.
    Network request … and showthe result fun getLocation(): Location { … } fun getWeather(location: Location): Weather { … } fun showWeather(weather: Weather) { /// shows weather on the screen } 2018 / KOTLIN COROUTINES IN ANDROID
  • 9.
    fun getLocationAsync(cb: (Location)-> Unit) { /// gets current location and returns it /// with callback when done } Callbacks We can do same with callbacks 2018 / KOTLIN COROUTINES IN ANDROID
  • 10.
    fun getLocationAsync(cb: (Location)-> Unit) { … } fun getWeatherAsync(location: Location, cb: (Weather) -> Unit) { /// gets weather for current location async } Callbacks We can do same with callbacks 2018 / KOTLIN COROUTINES IN ANDROID
  • 11.
    fun getLocationAsync(cb: (Location)-> Unit) { … } fun getWeatherAsync(location: Location, cb: (Weather) -> Unit) { … } fun showWeather(weather: Weather) { /// shows weather on the screen } Callbacks We can do same with callbacks 2018 / KOTLIN COROUTINES IN ANDROID
  • 12.
    fun getLocationAsync(cb: (Location)-> Unit) { … } fun getWeatherAsync(location: Location, cb: (Weather) -> Unit) { … } fun showWeather(weather: Weather) { … } fun processWeather() { getLocationAsync { location -> getWeatherAsync { weather -> showWeather(weather) } } } Callbacks We can do same with callbacks 2018 / KOTLIN COROUTINES IN ANDROID
  • 13.
    Suspending function Suspending function suspends thecoroutine, without blocking the thread suspend fun getLocation(): Location { /// gets current location & suspends return location } 2018 / KOTLIN COROUTINES IN ANDROID
  • 14.
    Suspending function Suspending function suspends thecoroutine, without blocking the thread suspend fun getLocation(): Location { … } suspend fun getWeather(location: Location): Weather { /// gets weather for current location & /// suspends return weather } 2018 / KOTLIN COROUTINES IN ANDROID
  • 15.
    Suspending function Suspending function suspends thecoroutine, without blocking the thread 2018 / KOTLIN COROUTINES IN ANDROID suspend fun getLocation(): Location { … } suspend fun getWeather(location: Location) : Weather { … } fun showWeather(weather: Weather) { /// shows weather on the screen } suspend fun processWeather() { val location = getLocation() val weather = getWeather(location) showWeather(weather) }
  • 16.
    2018 / KOTLINCOROUTINES IN ANDROID
  • 17.
    Launch coroutine /** Simple countdownwith coroutines */ fun countdown(start: Int) { launch(UI) { for (i in start downTo 0) { /// show time delay(1000) } } } 2018 / KOTLIN COROUTINES IN ANDROID
  • 18.
    Place your screenshot here 2018/ KOTLIN COROUTINES IN ANDROID
  • 19.
    Thanks! Any questions? You canfind me at me@robertlevonyan.com Or tweet @robertlevonyan