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
Roberto Orgiu
November 17, 2017
Technology
0
170
EAsync Kotlin
Slides about Kotlin Coroutines - Talk kept at DevFest Catania and DevFest Istanbul
Roberto Orgiu
November 17, 2017
Tweet
Share
More Decks by Roberto Orgiu
See All by Roberto Orgiu
Wellness & Droid
tiwiz
0
92
Behind the curtains
tiwiz
0
36
The Importance of Being Tested
tiwiz
0
360
An Android Dev start to Kotlin MPP
tiwiz
0
130
Fantastic API and where to find them
tiwiz
0
50
Flipping the Koin @ GDG Dev Party
tiwiz
1
37
Flipping the Koin
tiwiz
2
140
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
80
Trip into the async world
tiwiz
1
99
Other Decks in Technology
See All in Technology
アジリティを高めるテストマネジメント #QiitaQualityForward
makky_tyuyan
1
560
20250307_エンジニアじゃないけどAzureはじめてみた
ponponmikankan
2
280
あなたが人生で成功するための5つの普遍的法則 #jawsug #jawsdays2025 / 20250301 HEROZ
yoshidashingo
2
510
プロダクト開発者目線での Entra ID 活用
sansantech
PRO
0
280
"TEAM"を導入したら最高のエンジニア"Team"を実現できた / Deploying "TEAM" and Building the Best Engineering "Team"
yuj1osm
1
250
Quality with Angular: Tools and Processes
rainerhahnekamp
0
110
20250309 無冠のわたし これからどう先生きのこれる?
akiko_pusu
11
1.7k
Linuxのブートプロセス
sat
PRO
6
110
株式会社Awarefy(アウェアファイ)会社説明資料 / Awarefy-Company-Deck
awarefy
3
12k
開発組織を進化させる!AWSで実践するチームトポロジー
iwamot
2
650
Qiita Organizationを導入したら、アウトプッターが爆増して会社がちょっと有名になった件
minorun365
PRO
1
400
アウトカムを最大化させるプロダクトエンジニアの動き
hacomono
PRO
0
180
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
450
Statistics for Hackers
jakevdp
797
220k
Thoughts on Productivity
jonyablonski
69
4.5k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.9k
Producing Creativity
orderedlist
PRO
344
40k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Into the Great Unknown - MozCon
thekraken
35
1.7k
4 Signs Your Business is Dying
shpigford
183
22k
The Invisible Side of Design
smashingmag
299
50k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
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