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
180
kanmoba-returns-02.pdf
Takuji Nishibayashi
June 12, 2019
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
86
kotlinx.datetime 使ってみた
takuji31
0
330
HiltのCustom Componentについて
takuji31
0
190
java.timeをAndroidで使う
takuji31
0
74
KSPを使ってコード生成
takuji31
0
310
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
990
kotlinx.serialization
takuji31
0
550
AndroidXとKotlin Coroutines
takuji31
0
340
AndroidXに潜む便利なヤツら
takuji31
0
160
Other Decks in Technology
See All in Technology
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
3
2.4k
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
530
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
TSKaigi 2024 の登壇から広がったコミュニティ活動について
tsukuha
0
160
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
290
KnowledgeBaseDocuments APIでベクトルインデックス管理を自動化する
iidaxs
1
270
OpenAIの蒸留機能(Model Distillation)を使用して運用中のLLMのコストを削減する取り組み
pharma_x_tech
4
560
ハイテク休憩
sat
PRO
2
160
株式会社ログラス − エンジニア向け会社説明資料 / Loglass Comapany Deck for Engineer
loglass2019
3
32k
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Side Projects
sachag
452
42k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Practical Orchestrator
shlominoach
186
10k
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!