Slide 1

Slide 1 text

5෼ͰΘ͔ͬͨؾʹͳΔKotlin Corou,nes @takuji31 ؼ͖ͬͯͨؔ੢ϞόΠϧΞϓϦݚڀձ #2

Slide 2

Slide 2 text

ࣗݾ঺հ

Slide 3

Slide 3 text

ࣗݾ঺հ • @takuji31 id:takuji31 • Takuji Nishibayashi • Hatena Co., Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ

Slide 4

Slide 4 text

ࣗݾ঺հ • Android • Kotlin • Flu-er • ! h-ps:/ /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp

Slide 5

Slide 5 text

Kotlin Corou+nes

Slide 6

Slide 6 text

࢖ͬͯ·͔͢ʁ

Slide 7

Slide 7 text

ίϧʔνϯʢӳ: co-rou(neʣͱ͸ϓϩάϥ ϛϯάͷߏ଄ͷҰछɻαϒϧʔνϯ͕Τϯτ Ϧʔ͔ΒϦλʔϯ·ͰΛҰͭͷॲཧ୯Ґͱ͢ Δͷʹର͠ɺίϧʔνϯ͸͍ͬͨΜॲཧΛ தஅͨ͠ޙɺଓ͖͔ΒॲཧΛ࠶։Ͱ͖Δɻ — h$ps://ja.wikipedia.org/wiki/ %E3%82%B3%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3

Slide 8

Slide 8 text

Corou%neʹ͍͓ͭͯ͞Β͍͠· ͠ΐ͏

Slide 9

Slide 9 text

suspended func*on

Slide 10

Slide 10 text

தஅՄೳͳؔ਺

Slide 11

Slide 11 text

suspend function fetchUser() : User { val user = // ωοτϫʔΫ௨৴ͳͲͷॏ͍ॲཧ return user }

Slide 12

Slide 12 text

Corou%neContext

Slide 13

Slide 13 text

Corou%neͷίϯςΩετ

Slide 14

Slide 14 text

ෳ਺ͷElementΛ૊Έ߹Θͤͯ࡞Δ

Slide 15

Slide 15 text

Job

Slide 16

Slide 16 text

Dispatcher

Slide 17

Slide 17 text

Name

Slide 18

Slide 18 text

Excep&onHandler

Slide 19

Slide 19 text

ωετՄೳ

Slide 20

Slide 20 text

val context = Job() + Dispatchers.Main launch(context) { // run in main thread delay(100) val response = withContext(Dispatchers.Default) { // run in background thread networkAccess() } }

Slide 21

Slide 21 text

Corou%neScope

Slide 22

Slide 22 text

Corou%neͷείʔϓ

Slide 23

Slide 23 text

ContextΛ࣋ͭ

Slide 24

Slide 24 text

ωετՄೳ

Slide 25

Slide 25 text

async/launchͰ্ཱͪ͛ͨίϧʔν ϯ಺Ͱྫ֎͕ى͖Δͱείʔϓશମ ͕ࢮ͵ͷͰ஫ҙ

Slide 26

Slide 26 text

Ϋϥογϡ͢Δ΍ͭ // ྫ֎Ͱείʔϓશମ͕ࢮΜͰΫϥογϡ͢Δ coroutineScope { try { val user = async { fetchUser() } val series = async { fetchSeries("12345") } doSomething(user.await(), series.await()) } catch (e: IOException) { // catch͞Εͳ͍ } }

Slide 27

Slide 27 text

ରࡦ 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 } }

Slide 28

Slide 28 text

Deferred

Slide 29

Slide 29 text

ϊϯϒϩοΩϯάͳFuture

Slide 30

Slide 30 text

val user = async { fetchUser() } // Deferred val series = async { fetchSeries("12345") } // Deferred doSomething(user.await(), series.await())

Slide 31

Slide 31 text

Channel

Slide 32

Slide 32 text

ϗοτͳετϦʔϜ

Slide 33

Slide 33 text

val channel = Channel(Channel.CONFLATED) userObserver.addOnChangeListener { user -> launch { channel.send(user) } } channel.consumeEach { user -> doSomething(user) }

Slide 34

Slide 34 text

Flow

Slide 35

Slide 35 text

ίʔϧυͳετϦʔϜ

Slide 36

Slide 36 text

Preview status

Slide 37

Slide 37 text

1.3.0-M1ͰExperimentalʹ

Slide 38

Slide 38 text

val userFlow = flow { emit(observeUser()) } val seriesFlow = flow { emit(observeSeries()) } userFlow.combineLatest(seriesFlow) { user, series -> createItemModels(user, series) }.collect { itemModels -> // show items }

Slide 39

Slide 39 text

Enjoy Kotlin Corou.nes life!