Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
EAsync Kotlin
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Roberto Orgiu
November 17, 2017
Technology
220
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
EAsync Kotlin
Slides about Kotlin Coroutines - Talk kept at DevFest Catania and DevFest Istanbul
Roberto Orgiu
November 17, 2017
More Decks by Roberto Orgiu
See All by Roberto Orgiu
Wellness & Droid
tiwiz
0
140
Behind the curtains
tiwiz
0
94
The Importance of Being Tested
tiwiz
0
450
An Android Dev start to Kotlin MPP
tiwiz
0
210
Fantastic API and where to find them
tiwiz
0
110
Flipping the Koin @ GDG Dev Party
tiwiz
1
83
Flipping the Koin
tiwiz
2
190
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
130
Trip into the async world
tiwiz
1
160
Other Decks in Technology
See All in Technology
From Vanilla Kubernetes to a Batteries-Included Platform: Developer Experience at 1,300+ Clusters
yosshi_
0
610
『止めない』を設計する — 制約の中で、事業の根幹を支える判断
hiroyaterui
0
220
20260906 「AWS運用入門」著者が教える、運用業務への生成AI活用入門
masaruogura
0
240
データエンジニアの困りごとをDevinと一緒に解消する
10xinc
2
850
コスト最適化の「めんどくさい」を AWS FinOps Agent でチョット楽にする
classmethod_kaz
0
260
AIとペアプロを始める。人とのペアプロをやめる。ペアプロの良さを改めて知る。もっと好きになった。 / Rediscovering Pair Programming
honyanya
1
280
AIで実装は速くなった。なのにプロダクトは速くならない。職能の壁を越えて価値のフローを設計する
nwiizo
7
7.4k
AI時代のAPI品質を支えるガードレール / API Guardrails for API quality in the AI era
yokawasa
1
150
AIで開発は速くなったのに、なぜ現場は楽にならないのか 〜あなたの組織のボトルネックを突き止めるワークショップ〜
jacopen
1
270
V8コントリビュート超入門
riyaamemiya
0
140
module Synths; end
asonas
1
120
Tab5をRubyで動くパソコンにする
kishima
1
250
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Mobile First: as difficult as doing things right
swwweet
225
10k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Technical Leadership for Architectural Decision Making
baasie
3
560
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
520
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
The Cult of Friendly URLs
andyhume
79
7k
Six Lessons from altMBA
skipperchong
29
4.5k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
My Coaching Mixtape
mlcsv
0
310
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
600
Transcript
Roberto Orgiu @_tiwiz Async Kotlin
Roberto Orgiu @_tiwiz EAsync Kotlin
Roberto Orgiu @_tiwiz What we expect
Roberto Orgiu @_tiwiz What we get
Roberto Orgiu @_tiwiz
Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)
Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)
Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)
Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)
Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)
Roberto Orgiu @_tiwiz suspend fun delay( time: Long, unit: TimeUnit)
{ … }
Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)
Roberto Orgiu @_tiwiz launch { delay(1000L) println("World!") }T println("Hello,") Thread.sleep(2000L)
Roberto Orgiu @_tiwiz !
Roberto Orgiu @_tiwiz //gradle.properties kotlin.coroutines=enable
Roberto Orgiu @_tiwiz //gradle.properties kotlin.coroutines=enable dependencies { compile “org.jetbrains.kotlinx:kotlinx- coroutines-android:0.19.3"
}
Roberto Orgiu @_tiwiz //gradle.properties kotlin.coroutines=enable dependencies { compile “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.19.3" }
kotlin { experimental { coroutines "enable" } }
Roberto Orgiu @_tiwiz launch(UI){T for (i in 10 downTo 1)
{ hello.text = "Countdown $i ..." delay(500) }T hello.text="Done!" }T
Roberto Orgiu @_tiwiz launch( UI){T for (i in 10 downTo
1) { hello.text = "Countdown $i ..." delay(500) }T hello.text="Done!" }T
Roberto Orgiu @_tiwiz launch(UI){T for (i in 10 downTo 1)
{ hello.text = "Countdown $i ..." delay(500) }T hello.text="Done!" }T
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()
Roberto Orgiu @_tiwiz suspend fun TextView.countdown()_{_ for_(i_in 10 downTo 1)_{_
text = "Countdown $i ..." delay(1000) }_ text="Done!" }_
Roberto Orgiu @_tiwiz suspend fun TextView.countdown()_{_}_ fun View.onClick(action: suspend ()_
->_Unit)_{_ setOnClickListener { launch(UI) { action() } } }_
Roberto Orgiu @_tiwiz suspend fun TextView.countdown()_{_}_ fun View.onClick(action: suspend ()_
->_Unit)_{_}_ fab.onClick { hello.countdown() }
Roberto Orgiu @_tiwiz
Roberto Orgiu @_tiwiz
Roberto Orgiu @_tiwiz Coroutines + Retrofit
Roberto Orgiu @_tiwiz fun View.onClick(action: suspend () -> Deferred<Response>) {
setOnClickListener { launch(UI) { toggleWaitingView() val response = action().await() bindUiTo(response) toggleContentView() }T }T }T
Roberto Orgiu @_tiwiz fun View.onClick(action: suspend () -> Deferred<Response>) {
setOnClickListener { launch(UI) { toggleWaitingView() val response = action().await() bindUiTo(response) toggleContentView() }T }T }T
Roberto Orgiu @_tiwiz fun View.onClick(action: suspend () -> Deferred<Response>) {
setOnClickListener { launch(UI) { toggleWaitingView() val response = action().await() bindUiTo(response) toggleContentView() }T }T }T
Roberto Orgiu @_tiwiz suspend fun getWeatherAsync() =T async {T getWeatherSync()
}T
Roberto Orgiu @_tiwiz suspend fun getWeatherAsync() =T async {T getWeatherSync()
}T
Roberto Orgiu @_tiwiz Incoming official support
Roberto Orgiu @_tiwiz What about testing?
Roberto Orgiu @_tiwiz @Test fun coroutine_test() {T val actualWeather =
runBlocking {T getWeatherAsync().await() }T …T }T
Roberto Orgiu @_tiwiz @Test fun coroutine_test() {T val actualWeather =
runBlocking {T getWeatherAsync().await() }T …T }T
Roberto Orgiu @_tiwiz One more thing… TRICK
Roberto Orgiu @_tiwiz Coroutines can be lazy
Roberto Orgiu @_tiwiz val result =T async(start = CoroutineStart.LAZY) {…}T
Roberto Orgiu @_tiwiz val result =T async(start = CoroutineStart.LAZY) {…}T
result.await()
Roberto Orgiu @_tiwiz Should we use coroutines?
Roberto Orgiu @_tiwiz YES
Roberto Orgiu @_tiwiz
Roberto Orgiu @_tiwiz open.nytimes.com @nytdev github.com/NYTimes developers.nytimes.com
Roberto Orgiu @_tiwiz Thank you @_tiwiz
Roberto Orgiu @_tiwiz EAsync Kotlin
Roberto Orgiu @_tiwiz EAsync Kotlin
Roberto Orgiu @_tiwiz EAsync Kotlin bit.ly/EAsyncKotlin