Upgrade to Pro — share decks privately, control downloads, hide ads and more …

EAsync Kotlin

6923bdeb363961b064d2cdb6329982d6?s=47 Roberto Orgiu
November 17, 2017

EAsync Kotlin

Slides about Kotlin Coroutines - Talk kept at DevFest Catania and DevFest Istanbul

6923bdeb363961b064d2cdb6329982d6?s=128

Roberto Orgiu

November 17, 2017
Tweet

More Decks by Roberto Orgiu

Other Decks in Technology

Transcript

  1. Roberto Orgiu @_tiwiz Async Kotlin

  2. Roberto Orgiu @_tiwiz EAsync Kotlin

  3. Roberto Orgiu @_tiwiz What we expect

  4. Roberto Orgiu @_tiwiz What we get

  5. Roberto Orgiu @_tiwiz

  6. Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)

  7. Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)

  8. Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)

  9. Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)

  10. Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)

  11. Roberto Orgiu @_tiwiz suspend fun delay( time: Long, unit: TimeUnit)

    { … }
  12. Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)

  13. Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)

  14. Roberto Orgiu @_tiwiz !

  15. Roberto Orgiu @_tiwiz //gradle.properties kotlin.coroutines=enable

  16. Roberto Orgiu @_tiwiz //gradle.properties kotlin.coroutines=enable dependencies { compile “org.jetbrains.kotlinx:kotlinx- coroutines-android:0.19.3"

    }
  17. Roberto Orgiu @_tiwiz //gradle.properties kotlin.coroutines=enable dependencies { compile “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.19.3" }

    kotlin { experimental { coroutines "enable" } }
  18. Roberto Orgiu @_tiwiz launch(UI){T for (i in 10 downTo 1)

    { hello.text = "Countdown $i ..." delay(500) }T hello.text="Done!" }T
  19. Roberto Orgiu @_tiwiz launch( UI){T for (i in 10 downTo

    1) { hello.text = "Countdown $i ..." delay(500) }T hello.text="Done!" }T
  20. Roberto Orgiu @_tiwiz launch(UI){T for (i in 10 downTo 1)

    { hello.text = "Countdown $i ..." delay(500) }T hello.text="Done!" }T
  21. Roberto Orgiu @_tiwiz launch(UI){T for (i in 10 downTo 1)

    { hello.text = "Countdown $i ..." delay(500) }T hello.text="Done!" }T val job = job.cancel()
  22. Roberto Orgiu @_tiwiz suspend fun TextView.countdown()_{_ for_(i_in 10 downTo 1)_{_

    text = "Countdown $i ..." delay(1000) }_ text="Done!" }_
  23. Roberto Orgiu @_tiwiz suspend fun TextView.countdown()_{_}_ fun View.onClick(action: suspend ()_

    ->_Unit)_{_ setOnClickListener { launch(UI) { action() } } }_
  24. Roberto Orgiu @_tiwiz suspend fun TextView.countdown()_{_}_ fun View.onClick(action: suspend ()_

    ->_Unit)_{_}_ fab.onClick { hello.countdown() }
  25. Roberto Orgiu @_tiwiz

  26. Roberto Orgiu @_tiwiz

  27. Roberto Orgiu @_tiwiz Coroutines + Retrofit

  28. Roberto Orgiu @_tiwiz fun View.onClick(action: suspend () -> Deferred<Response>) {

    setOnClickListener { launch(UI) { toggleWaitingView() val response = action().await() bindUiTo(response) toggleContentView() }T }T }T
  29. Roberto Orgiu @_tiwiz fun View.onClick(action: suspend () -> Deferred<Response>) {

    setOnClickListener { launch(UI) { toggleWaitingView() val response = action().await() bindUiTo(response) toggleContentView() }T }T }T
  30. Roberto Orgiu @_tiwiz fun View.onClick(action: suspend () -> Deferred<Response>) {

    setOnClickListener { launch(UI) { toggleWaitingView() val response = action().await() bindUiTo(response) toggleContentView() }T }T }T
  31. Roberto Orgiu @_tiwiz suspend fun getWeatherAsync() =T async {T getWeatherSync()

    }T
  32. Roberto Orgiu @_tiwiz suspend fun getWeatherAsync() =T async {T getWeatherSync()

    }T
  33. Roberto Orgiu @_tiwiz Incoming official support

  34. Roberto Orgiu @_tiwiz What about testing?

  35. Roberto Orgiu @_tiwiz @Test fun coroutine_test() {T val actualWeather =

    runBlocking {T getWeatherAsync().await() }T …T }T
  36. Roberto Orgiu @_tiwiz @Test fun coroutine_test() {T val actualWeather =

    runBlocking {T getWeatherAsync().await() }T …T }T
  37. Roberto Orgiu @_tiwiz One more thing… TRICK

  38. Roberto Orgiu @_tiwiz Coroutines can be lazy

  39. Roberto Orgiu @_tiwiz val result =T async(start = CoroutineStart.LAZY) {…}T

  40. Roberto Orgiu @_tiwiz val result =T async(start = CoroutineStart.LAZY) {…}T

    result.await()
  41. Roberto Orgiu @_tiwiz Should we use coroutines?

  42. Roberto Orgiu @_tiwiz YES

  43. Roberto Orgiu @_tiwiz

  44. Roberto Orgiu @_tiwiz open.nytimes.com @nytdev github.com/NYTimes developers.nytimes.com

  45. Roberto Orgiu @_tiwiz Thank you @_tiwiz

  46. Roberto Orgiu @_tiwiz EAsync Kotlin

  47. Roberto Orgiu @_tiwiz EAsync Kotlin

  48. Roberto Orgiu @_tiwiz EAsync Kotlin bit.ly/EAsyncKotlin