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
kanmoba-returns-02.pdf
Search
Takuji Nishibayashi
June 12, 2019
Technology
0
220
kanmoba-returns-02.pdf
Takuji Nishibayashi
June 12, 2019
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
compose-hot-reload を試そうとした話
takuji31
0
71
CameraX使ってみた
takuji31
0
210
kotlinx.datetime 使ってみた
takuji31
0
660
HiltのCustom Componentについて
takuji31
0
270
java.timeをAndroidで使う
takuji31
0
130
KSPを使ってコード生成
takuji31
0
370
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
2.7k
kotlinx.serialization
takuji31
0
610
AndroidXとKotlin Coroutines
takuji31
0
380
Other Decks in Technology
See All in Technology
Clineを含めたAIエージェントを 大規模組織に導入し、投資対効果を考える / Introducing AI agents into your organization
i35_267
1
370
DB 醬,嗨!哪泥嘎斯基?
line_developers_tw
PRO
0
240
Whats_new_in_Podman_and_CRI-O_2025-06
orimanabu
3
180
OpenTelemetry Collector internals
ymotongpoo
5
560
Workflows から Agents へ ~ 生成 AI アプリの成長過程とアプローチ~
belongadmin
3
160
開発効率と信頼性を両立する Ubieのプラットフォームエンジニアリング
teru0x1
0
150
Web3 のリアリティ / Web3 Reality
ks91
PRO
0
100
成立するElixirの再束縛(再代入)可という選択
kubell_hr
0
360
(新URLに移行しました)FASTと向き合うことで見えた、大規模アジャイルの難しさと楽しさ
wooootack
0
740
"SaaS is Dead" は本当か!? 生成AI時代の医療 Vertical SaaS のリアル
kakehashi
PRO
3
230
Autonomous Database サービス・アップデート (FY25)
oracle4engineer
PRO
2
780
CIでのgolangci-lintの実行を約90%削減した話
kazukihayase
0
310
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Code Reviewing Like a Champion
maltzj
524
40k
The World Runs on Bad Software
bkeepers
PRO
68
11k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
The Invisible Side of Design
smashingmag
299
51k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
660
Gamification - CAS2011
davidbonilla
81
5.3k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Transcript
5ͰΘ͔ͬͨؾʹͳΔKotlin Corou,nes @takuji31 ؼ͖ͬͯͨؔϞόΠϧΞϓϦݚڀձ #2
ࣗݾհ
ࣗݾհ • @takuji31 id:takuji31 • Takuji Nishibayashi • Hatena Co.,
Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ
ࣗݾհ • Android • Kotlin • Flu-er • ! h-ps:/
/blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
Kotlin Corou+nes
ͬͯ·͔͢ʁ
ίϧʔνϯʢӳ: co-rou(neʣͱϓϩάϥ ϛϯάͷߏͷҰछɻαϒϧʔνϯ͕Τϯτ Ϧʔ͔ΒϦλʔϯ·ͰΛҰͭͷॲཧ୯Ґͱ͢ Δͷʹର͠ɺίϧʔνϯ͍ͬͨΜॲཧΛ தஅͨ͠ޙɺଓ͖͔ΒॲཧΛ࠶։Ͱ͖Δɻ — h$ps://ja.wikipedia.org/wiki/ %E3%82%B3%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3
Corou%neʹ͍͓ͭͯ͞Β͍͠· ͠ΐ͏
suspended func*on
தஅՄೳͳؔ
suspend function fetchUser() : User { val user = //
ωοτϫʔΫ௨৴ͳͲͷॏ͍ॲཧ return user }
Corou%neContext
Corou%neͷίϯςΩετ
ෳͷElementΛΈ߹Θͤͯ࡞Δ
Job
Dispatcher
Name
Excep&onHandler
ωετՄೳ
val context = Job() + Dispatchers.Main launch(context) { // run
in main thread delay(100) val response = withContext(Dispatchers.Default) { // run in background thread networkAccess() } }
Corou%neScope
Corou%neͷείʔϓ
ContextΛ࣋ͭ
ωετՄೳ
async/launchͰ্ཱͪ͛ͨίϧʔν ϯͰྫ֎͕ى͖Δͱείʔϓશମ ͕ࢮ͵ͷͰҙ
Ϋϥογϡ͢Δͭ // ྫ֎Ͱείʔϓશମ͕ࢮΜͰΫϥογϡ͢Δ coroutineScope { try { val user =
async { fetchUser() } val series = async { fetchSeries("12345") } doSomething(user.await(), series.await()) } catch (e: IOException) { // catch͞Εͳ͍ } }
ରࡦ coroutineScope { try { // Nested scope val (user,
series) = coroutineScope { val user = async { fetchUser() } val series = async { fetchSeries("12345") } Pair(user, series) } doSomething(user.await(), series.await()) } catch (e: IOException) { // show error } }
Deferred
ϊϯϒϩοΩϯάͳFuture
val user = async { fetchUser() } // Deferred<User> val
series = async { fetchSeries("12345") } // Deferred<Series> doSomething(user.await(), series.await())
Channel
ϗοτͳετϦʔϜ
val channel = Channel<User>(Channel.CONFLATED) userObserver.addOnChangeListener { user -> launch {
channel.send(user) } } channel.consumeEach { user -> doSomething(user) }
Flow
ίʔϧυͳετϦʔϜ
Preview status
1.3.0-M1ͰExperimentalʹ
val userFlow = flow<User> { emit(observeUser()) } val seriesFlow =
flow<Series> { emit(observeSeries()) } userFlow.combineLatest(seriesFlow) { user, series -> createItemModels(user, series) }.collect { itemModels -> // show items }
Enjoy Kotlin Corou.nes life!