Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
EAsync Kotlin
Roberto Orgiu
November 17, 2017
Technology
0
140
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
10
Behind the curtains
tiwiz
0
11
The Importance of Being Tested
tiwiz
0
130
An Android Dev start to Kotlin MPP
tiwiz
0
53
Fantastic API and where to find them
tiwiz
0
26
Flipping the Koin @ GDG Dev Party
tiwiz
1
27
Flipping the Koin
tiwiz
2
88
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
49
Trip into the async world
tiwiz
1
62
Other Decks in Technology
See All in Technology
AWS ChatbotでEC2インスタンスを 起動できるようにした
iwamot
0
120
CADDi HCMC Technology Center
caddi_eng
0
250
THETA Xの登場はジオ業界を変えるか?
furuhashilab
0
160
モダンデータスタックとかの話(データエンジニアのお仕事とは)
foursue
0
390
ISUCON で使えるツールを作った
shotakitazawa
0
350
ITエンジニアを取り巻く環境とキャリアパス / A career path for Japanese IT engineers
takatama
0
580
Research Paper Introduction #98 "NSDI 2022 recap"
cafenero_777
0
200
プロダクトグロースと技術のベースアップを両立させるRettyのアプリ開発スタイル / Achieve Product Growth and Tech Update
imaizume
1
290
HTTP Session Architecture Pattern
chiroito
1
400
暗号資産ウォレット入門(MetaMaskの入門~NFTの購入~詐欺の注意事項など)
kayato
2
190
toilを解消した話
asumaywy
0
170
Building smarter apps with machine learning, from magic to reality
picardparis
4
3.1k
Featured
See All Featured
Making Projects Easy
brettharned
98
4.3k
The Brand Is Dead. Long Live the Brand.
mthomps
45
2.7k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
315
19k
Stop Working from a Prison Cell
hatefulcrawdad
261
17k
The Web Native Designer (August 2011)
paulrobertlloyd
74
1.9k
Product Roadmaps are Hard
iamctodd
34
6.1k
Visualization
eitanlees
124
11k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
350
21k
Reflections from 52 weeks, 52 projects
jeffersonlam
337
17k
GraphQLの誤解/rethinking-graphql
sonatard
24
6.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
4
2k
10 Git Anti Patterns You Should be Aware of
lemiorhan
638
52k
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