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
37
The Importance of Being Tested
tiwiz
0
370
An Android Dev start to Kotlin MPP
tiwiz
0
130
Fantastic API and where to find them
tiwiz
0
51
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
81
Trip into the async world
tiwiz
1
100
Other Decks in Technology
See All in Technology
Go Modulesの仕組み Bundler(Ruby)との比較を添えて
daisuketakeda
0
1.8k
Cloudflare Pages 4年使って分かった良さと注意点
kyosuke
0
210
Autonomous Database - Dedicated 技術詳細 / adb-d_technical_detail_jp
oracle4engineer
PRO
4
8.1k
Microsoft_20250311_第2回AI_IoT共創ラボ.pdf
iotcomjpadmin
0
440
もうVPNは古い? VPNを使わずに オンプレサーバーを 管理する手法あれこれ
ebibibi
0
150
Webブラウザのセキュリティ対策に役立つぞ!!~DevToolsの使い方~
masakiokuda
0
160
TDSE_20250311_AIxIoTビジネス共創ラボ第2回勉強会_発表資料.pdf
iotcomjpadmin
0
240
AI_Agent_の作り方_近藤憲児
kenjikondobai
19
5.3k
Kubernetesを手元で学ぼう! 初心者向けローカル環境のススメ
nayaaaa
PRO
2
810
ランチの間に GitHub Copilot Agent が仕事を終わらせてくれた話
bicstone
5
580
なぜ「Event Sourcing」を選択したのか〜事実に基づくことの重要性〜/Why did we choose "Event Sourcing"?
bitkey
1
330
痒い所に手が届く!要約モデルのつくり方
sakusakumura
1
280
Featured
See All Featured
For a Future-Friendly Web
brad_frost
176
9.6k
Product Roadmaps are Hard
iamctodd
PRO
51
11k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
176
52k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Visualization
eitanlees
146
15k
The Pragmatic Product Professional
lauravandoore
32
6.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Building Applications with DynamoDB
mza
93
6.3k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
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