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
触って分かるコルーチン
Search
izmeal2000
February 13, 2019
0
590
触って分かるコルーチン
izmeal2000
February 13, 2019
Tweet
Share
More Decks by izmeal2000
See All by izmeal2000
実践、BFF ~ BFFはFiNCのアプリで何を解決したのか ~
kensukeizumi
5
5k
Androidの歩数取得とバックグラウンド制限
kensukeizumi
0
4.1k
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
48
7.1k
5 minutes of I Can Smell Your CMS
philhawksworth
201
19k
Optimising Largest Contentful Paint
csswizardry
29
2.8k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
278
13k
In The Pink: A Labor of Love
frogandcode
139
22k
Web Components: a chance to create the future
zenorocha
308
41k
Pencils Down: Stop Designing & Start Developing
hursman
118
11k
Fireside Chat
paigeccino
31
2.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
401
65k
The Invisible Side of Design
smashingmag
295
50k
Clear Off the Table
cherdarchuk
90
320k
The Language of Interfaces
destraynor
153
23k
Transcript
৮ͬͯΘ͔ΔCoroutine FiNC Technologies Inc. / Engineer Development Kensuke Izumi
ࣗݾհ Kensuke Izumi Android Engineer @FiNC Kotlin / javascript /
ruby Twitter: @izmeal2000
͍͔ʹͯ͠reject͞Εͨͷ͔
͍͔ʹͯ͠reject͞Εͨͷ͔ νʔϜͰͷෳճͷϨϏϡʔΛܦͯ ϒϥογϡΞοϓ͞ΕͨCFP!
͍͔ʹͯ͠reject͞Εͨͷ͔ ͗Γ͗Γ·Ͱ೪ͬͯमਖ਼ʹमਖ਼ΛॏͶͯSUBMIT!!! At 23:59 2018/10/21
͍͔ʹͯ͠reject͞Εͨͷ͔
͍͔ʹͯ͠reject͞Εͨͷ͔ Rejected by System
ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
What is Coroutine?
What is Coroutine? Coroutines are light weight threads
What is Coroutine? Coroutines are light weight threads ?
ු͔ΜͰ͖ͨٙ Coroutines are light weight threads ίϧʔνϯܰྔεϨου
ු͔ΜͰ͖ͨٙ Coroutines are light weight threads CoroutineͬͯThreadͱରʹൺֱͯ͠ྑ͍ͷͳͷʁ ݁ہJVMεϨου্Ͱ࣮ߦ͞ΕΔͷͰʁ
ͦͦThreadͬͯɺɺɺ ͕Android։ൃΛ࢝Ίͨ࣌ͷঢ়گ ඇಉظॲཧʁɹRXʂ
ͦͦThreadͬͯɺɺɺ ͕Android։ൃΛ࢝Ίͨ࣌ͷঢ়گ ඇಉظॲཧʁɹRXʂ Thread? observeOnͱ͔Ͱࢦఆ͢Δͭʂ
Threadͷࡶͳೝࣝ Threadͷ෮श͔Β࢝ΊΑ͏
ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
Androidʹ͓͚ΔThreadͷ෮श Androidͷ̐ͭͷίϯϙʔωϯτͱϓϩηε ɾActivity ɾService ɾBroadcastReceiver ɾContent Providers ɹ (جຊతʹ) ΞϓϦҰͭͷϓϩηεΛىಈ͍ͯͯ͠ɺϚϧνεϨουͰಈ͘
Androidʹ͓͚ΔThreadͷ෮श Androidʹ͓͚ΔThreadૢ࡞ͷݪଇ 1. Do not block the UI thread ɹඳը͕ࢭ·ΔͱΫϦςΟΧϧ
2. Do not access the Android UI toolkit from outside the UI thread ɹUI toolkitεϨουηʔϑͰແ͍ ɹ ࢀߟɿhttps://developer.android.com/guide/components/processes-and-threads
21 Androidʹ͓͚ΔThreadͷ෮श Androidʹ͓͚ΔϚϧνεϨουͷ੍ ɾωοτϫʔΫॲཧDBͷॻ͖ࠐΈͳͲΛߦ͏ࡍʹɺҟͳΔεϨου ʹॲཧΛͤΔඞཁ͕͋Δ ɾඳըॲཧͷࡍʹUIεϨουʹॲཧΛ͢ඞཁ͕͋Δ
22 ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
23 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html
24 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࣮ߦ͢Δͱ…ʁ A. Start End Hello Stop
B. Start Hello End Stop
25 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ A. Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html
26 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html Coroutine
27 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ
Coroutine
28 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html Coroutine
29 CoroutineͱThreadͷؔ ͱΓ͋͑ͣಈ͔ͯ͠ΈΔ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࠶։
Coroutine
30 CoroutineͱThreadͷؔ Ͳ͜Ͱಈ͍͍ͯΔͷ͔͔֬ΊΔ ࣮ߦ݁Ռ Start thread is main End thread
is DefaultDispatcher-worker-1 Hello Stop
31 CoroutineͱThreadͷؔ Ͳ͜Ͱಈ͍͍ͯΔͷ͔͔֬ΊΔ ࣮ߦ݁Ռ Start thread is main End thread
is DefaultDispatcher-worker-1 Hello Stop
CoroutineͱThreadͷؔ CoroutineɺThread্Ͱಈ͍͍ͯΔΜͩͶ (ࢸۃͨΓલͳͰ͋Δ)
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action தஅΛࣔ͢म০ࢠ
35 CoroutineͱThreadͷؔ suspendम০ࢠ ࣮ߦ݁Ռ Start End Hello Stop ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ
Coroutine
36 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html suspendम০ࢠ தஅΛࣔ͢म০ࢠ
37 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html 1ඵதஅʂ Coroutine
38 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ଓߦʂ ผεϨουͰ࣮ߦதʂ
39 CoroutineͱThreadͷؔ ࢀߟɿhttps://kotlinlang.org/docs/reference/coroutines/basics.html ࠶։
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action தஅΛࣔ͢म০ࢠ
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action ࣮ߦ݁Ռ 1 threads active
at the start 4 threads active at the end Took 1031 ms
CoroutineͱThreadͷؔ ͨ͘͞Μಈ͔ͯ͠ΈΔ ࢀߟɿLearning Coroutine in Action ࣮ߦ݁Ռ 1 threads active
at the start 4 threads active at the end Took 1031 ms 1ສݸͷίϧʔνϯʹରͯ͠εϨου3ͭ
CoroutineͱThreadͷؔ CoroutineͱThreadͷؔ ͦΕͧΕͷCoroutineThreadʹΞαΠϯ͞ΕΔɻ CoroutineΛ͏ͱɺগͳ͍ThreadͷੜͰฒߦॲཧΛߦ͏͜ͱ͕Մೳ
CoroutineͱThreadͷؔ ͦΕͧΕͷCoroutineThreadʹΞαΠϯ͞ΕΔɻ CoroutineΛ͏ͱɺগͳ͍ThreadͷੜͰฒߦॲཧΛߦ͏͜ͱ͕Մೳ suspendम০ࢠΛ༻͍ͨίϧʔνϯͷதஅͱɺ࣮ߦεϨουͷղ์
46 CoroutineͱThreadͷؔ ͬͪ͜ͷํ͕Θ͔Γ͍͢ʁ Coroutines are light weight threads => Coroutine
is a light way to do concurrency with threads
ࢀߟ threads are to coroutines what CPU cores are to
threads ࢀߟɿhttps//stackoverflow.com/questions/49606471/why-and-when-to-use-co-routines- instead-of-threads-in-android-using-kotlin-as-th Coroutineʹͱͬͯthreadͱɺ threadʹͱͬͯͷCPUͱಉ͡Α͏ͳͷͰ͋Δ
48 ΞδΣϯμ 1. Coroutineॳ৺ऀʹཱ͔ͪͩΔղͳఆٛͨͪ 2. Androidʹ͓͚ΔThreadͷ෮श 3. CoroutineͱThreadͷؔ 4. Coroutineͷղܾ͢Δ՝
Coroutineͷղܾ͢Δ՝ Coroutineɺগͳ͍εϨουͰޮతʹฒߦॲཧΛ͚͞Δʂ
Coroutineͷղܾ͢Δ՝ Coroutineɺগͳ͍εϨουͰޮతʹฒߦॲཧΛ͚͞Δʂ ͦͦAndroidͰͦΜͳʹಉ࣌ʹ࣮ߦ͢Δ͜ͱ͋Δͷ…? ଞʹྑ͍͜ͱͳ͍ͷ…?
Coroutineͷղܾ͢Δ՝ KotlinʹCoroutine͕ಋೖ͞ΕΔલͷ Android։ൃʹ͓͚Δඇಉظॲཧ ɾHandlerThread / AsyncTask ɾRxJava
Coroutineͷղܾ͢Δ՝ HandlerThread / AsyncTask ࢀߟɿhttps://academy.realm.io/jp/posts/android-thread-looper-handler/
Coroutineͷղܾ͢Δ՝ HandlerThread / AsyncTask ɾCallback hell ɾLifecycleʹ߹ΘͤͨϝϞϦཧ
Coroutineͷղܾ͢Δ՝ RxJava
Coroutineͷղܾ͢Δ՝ RxJava ίʔϧόοΫͷଟॏωετϝϞϦཧଟগղܾ͞Εͨ ͕ɺେ͛͞ ͦͦStreamѻ͏ͨΊͷlibrary
Coroutineͷղܾ͢Δ՝ Coroutine
Coroutineͷղܾ͢Δ՝ Coroutine readableͰγϯϓϧͳඇಉظॲཧ ɾno more callback hell ɾno more rxͷཞ༻
ɾஞ࣍ॲཧͰͷهड़
·ͱΊ ɾCoroutine is a light way to concurrency with threads
ɾܰྔͰฒߦॲཧʂ ɾreadableͰγϯϓϧͳฒߦॲཧʂ
·ͱΊ खΛಈ͔ͯ͠ྑ͍KotlinϥΠϑΛʂ https://kotlinlang.org/