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
180
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
100
Behind the curtains
tiwiz
0
46
The Importance of Being Tested
tiwiz
0
390
An Android Dev start to Kotlin MPP
tiwiz
0
140
Fantastic API and where to find them
tiwiz
0
59
Flipping the Koin @ GDG Dev Party
tiwiz
1
46
Flipping the Koin
tiwiz
2
140
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
90
Trip into the async world
tiwiz
1
110
Other Decks in Technology
See All in Technology
DB 醬,嗨!哪泥嘎斯基?
line_developers_tw
PRO
0
930
Azure AI Foundryでマルチエージェントワークフロー
seosoft
0
130
Uniadex__公開版_20250617-AIxIoTビジネス共創ラボ_ツナガルチカラ_.pdf
iotcomjpadmin
0
130
DenoとJSRで実現する最速MCPサーバー開発記 / Building MCP Servers at Lightning Speed with Deno and JSR
yamanoku
1
190
kubellが挑むBPaaSにおける、人とAIエージェントによるサービス開発の最前線と技術展望
kubell_hr
1
380
新卒3年目の後悔〜機械学習モデルジョブの運用を頑張った話〜
kameitomohiro
0
350
脅威をモデリングしてMCPのセキュリティ対策を考えよう
flatt_security
5
1.8k
評価の納得感を2段階高める「構造化フィードバック」
aloerina
1
280
Perk アプリの技術選定とリリースから1年弱経ってのふりかえり
stomk
0
120
Workflows から Agents へ ~ 生成 AI アプリの成長過程とアプローチ~
belongadmin
3
170
OAuth/OpenID Connectで実現するMCPのセキュアなアクセス管理
kuralab
5
640
今からでも間に合う! 生成AI「RAG」再入門 / Re-introduction to RAG in Generative AI
hideakiaoyagi
1
200
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
245
12k
Designing for Performance
lara
609
69k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Why Our Code Smells
bkeepers
PRO
337
57k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
How to train your dragon (web standard)
notwaldorf
92
6.1k
YesSQL, Process and Tooling at Scale
rocio
172
14k
The Cost Of JavaScript in 2023
addyosmani
50
8.4k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
660
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.7k
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