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
64
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
59
KSPを使ってコード生成
takuji31
0
290
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
960
kotlinx.serialization
takuji31
0
540
AndroidXとKotlin Coroutines
takuji31
0
330
AndroidXに潜む便利なヤツら
takuji31
0
160
Other Decks in Technology
See All in Technology
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
The Role of Developer Relations in AI Product Success.
giftojabu1
0
120
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
250
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
7
800
複雑なState管理からの脱却
sansantech
PRO
1
140
Terraform未経験の御様に対してどの ように導⼊を進めていったか
tkikuchi
2
430
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
2
590
安心してください、日本語使えますよ―Ubuntu日本語Remix提供休止に寄せて― 2024-11-17
nobutomurata
1
990
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.6k
TypeScript、上達の瞬間
sadnessojisan
46
13k
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
300
Featured
See All Featured
A better future with KSS
kneath
238
17k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Rails Girls Zürich Keynote
gr2m
94
13k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Automating Front-end Workflow
addyosmani
1366
200k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Fireside Chat
paigeccino
34
3k
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!