Share
Embed
Androidと非同期処理 とCoroutine1.0.0
- Kotlin1.3とCoroutine1.0.0とJetpack
- なぜAndroidでコルーチンを使いたいか
- Androidでコルーチンを使う準備
- 並行処理を書く(直列、並列、スコープと例外)
- おまけ1: Rx with Coroutine
- おまけ2: Jetpack with Coroutine
yagi
December 18, 2018
Transcript
143 slides
-
©2018 Lang-8 Inc. ALL Rights Reserved. w ,PUMJOͱ$PSPVUJOFͱ+FUQBDL w ͳͥ"OESPJEͰίϧʔνϯΛ͍͍͔ͨ
w "OESPJEͰίϧʔνϯΛ͏४උ w ฒߦॲཧΛॻ͘ ྻɺฒྻɺείʔϓͱྫ֎ w ͓·͚3YXJUI$PSPVUJOF w ͓·͚+FUQBDLXJUI$PSPVUJOF ࠓ͢͜ͱ -
©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJO w$PSPVUJOF 4UBCMF
w,PUMJO/BUJWF#FUB w,UPS#FUB wΠϯϥΠϯΫϥεɺίϯτϥΫτɺXIFO ࣜͷαϒδΣΫτFUDʜ -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF w$PSPVUJOF4DPQFΛಋೖ͠طଘͷॻ͖ํ͕ EFQSFDBUFEʹ d
w%JTQBUDIFSTΛಋೖ d wείʔϓͷྫ֎ͷ༷͕มΘͬͨ d wFYQFSJNFOUBMQBDLBHFΛഇࢭ d IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTSFMFBTFT -
©2018 Lang-8 Inc. ALL Rights Reserved. w7JFXͷJOqBUF NFBTVSF MBZPVU Ϩϯ
μϦϯάɺλονΠϕϯτͳͲදࣔʹؔΘΔ ॲཧΛ͢Δ w)[ͳΒϑϨʔϜNT͔͠ͳ͍ ϑϨʔ ϜམͪମݧΛଛͶΔ w࣌ؒͷϒϩοΩϯά"/3ʹͳΔ "OESPJEͱ6*εϨου -
©2018 Lang-8 Inc. ALL Rights Reserved. w$36% w%BUBCBTFɺ'JMF*0 w4ZOD w/FUXPSL*0
w$PNQVUF wෳࡶͳܭࢉ ΄ͱΜͲͷΞϓϦ͕ߦ͏ॏ͍ͨॲཧ -
©2018 Lang-8 Inc. ALL Rights Reserved. w"TZOD5BTL w&YFDVUPST ͔ͳΓϨΠϠʔ
w-PBEFST ݱࡏഇࢭ w'VUVSF NJO4EL7FSTJPOҎ্ w-JCSBSJFT 3Y+BWBɺ࣮࣭σϑΝΫτελϯμʔυ "OESPJEͷඇಉظॲཧ -
©2018 Lang-8 Inc. ALL Rights Reserved. εϨουͷ࣮ߦΠϝʔδ 5ISFBE1PPM 8PSLFS 8PSLFS
8PSLFS 8PSLFS εϨου .#d.#΄ ͲϝϞϦΛ͏ -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯͷ࣮ߦΠϝʔδ 5ISFBE1PPM 8PSLFS 8PSLFS
8PSLFS 8PSLFS $PSPVUJOF ʜ ʜ εϨου .#d.#΄ ͲϝϞϦΛ͏ ͭͷίϧʔν ϯͰ,#΄Ͳɻ εϨουͰෳ ࣮ߦ͢Δɻ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } "1*ʹϦΫΤετͯ݁͠ՌΛฦؔ͢ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } ྃ࣌ͷίʔϧόοΫΛҾʹऔΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } ίʔϧόοΫʹଓ͖ͷॲཧΛॻ͘ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProfile(profile) } } ίʔϧόοΫʹଓ͖ͷॲཧΛॻ͘ ܧଓঢ়گΛ࣋ͭϓϩάϥϜ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜΛ༰қʹهड़͢Δ suspend fun getProfile(id:
Int): Profile suspend fun loadProfile(id: Int) { val profile = getProfile(token) showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜΛ༰қʹهड़͢Δ suspend fun getProfile(id:
Int): Profile suspend fun loadProfile(id: Int) { val profile = getProfile(token) showProfile(profile) } ϫʔΧʔεϨουͰಈ࡞ 6*εϨουͰಈ࡞ -
©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥίϧʔνϯΛ͍͍͔ͨ wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ w εϨουd.#ʹରͯ͠ɺίϧʔνϯ,#ఔ
w؆୯ʹ͑Δ ಡΈॻ͖ͷ͢͠͞ wܰྔͳϥΠϒϥϦ -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϥΠϒϥϦͰఏڙ͞Ε͍ͯΔ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1' implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.0.1' } -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϥΠϒϥϦͰఏڙ͞Ε͍ͯΔ dependencies { implementation
'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1' implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.0.1' } wKBS,# wBQL,# wPQUJNJ[F࠷খ,# -
©2018 Lang-8 Inc. ALL Rights Reserved. ͳͥίϧʔνϯΛ͍͍͔ͨ wݶΒΕͨϦιʔεͰεέʔϧͰ͖Δ w εϨουd.#ʹରͯ͠ɺίϧʔνϯ,#ఔ
w؆୯ʹ͑Δ ಡΈॻ͖ͷ͢͠͞ wܰྔͳϥΠϒϥϦ w KBS,# BQL,# PQUJNJ[F࠷খ,# -
©2018 Lang-8 Inc. ALL Rights Reserved. w$PSPVUJOF4DPQFίϧʔνϯͷ࣮ߦڥ wىಈͨ͠ίϧʔνϯ$PSPVUJOF4DPQFͷ ࢠʹͳΔ w$PSPVUJOF4DPQF͕ऴྃ͢Δͱࢠίϧʔν
ϯऴྃ͢Δ wϥΠϑαΠΫϧʹ߹Θͤͯ$PSPVUJOF4DPQF Λ༻ҙ͢Δ $PSPVUJOF4DPQFΛ༻ҙ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job public interface CoroutineScope { public val coroutineContext: CoroutineContext } -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job ࣮ߦڥΛઃఆ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. %JTQBUDIFST.BJO wϝΠϯεϨουͰ࣮ߦ͢Δ w"OESPJEͷ߹6*εϨου )BOEMFS
wLPUMJOYDPSPVUJOFTBOESPJE͕ඞཁ -
©2018 Lang-8 Inc. ALL Rights Reserved. %JTQBUDIFST*0 w*0ʹ࠷దԽͨ͠εϨουϓʔϧ w࠷ฒߦ w%FGBVMUͱεϨουϓʔϧͱڞ༗͍ͯ͠
ΔͷͰߴ Β͍͠ˣ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTCMPCNBTUFSDPSFLPUMJOYDPSPVUJOFTDPSFTSDTDIFEVMJOH$PSPVUJOF4DIFEVMFSLU -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job ࢠίϧʔνϯͷϋϯυϥ -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } } ίϧʔνϯΛىಈ -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } } NTఀࢭͯ͠࠶։͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch { var count = 0 while (true) { delay(1000) println("hi! ${count++}") } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ override fun onPause()
{ job.cancelChildren() super.onPause() } -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ override fun onPause()
{ job.cancelChildren() super.onPause() } ࢠίϧʔνϯΛͯ͢ Ωϟϯηϧ ऴྃ ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ override fun onPause()
{ job.cancelChildren() super.onPause() } -
©2018 Lang-8 Inc. ALL Rights Reserved. $PSPVUJOF4DPQFΛ࣮͢Δ class MainActivity :
AppCompatActivity(), CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onResume() { super.onResume() launch {/*…*/} } override fun onPause() { job.cancelChildren() super.onPause() } } -
©2018 Lang-8 Inc. ALL Rights Reserved. "OESPJEͰίϧʔνϯΛ͏४උ wίϧʔνϯΛ࣮ߦ͢Δʹ $PSPVUJOF4DPQF͕ඞཁ wϥΠϑαΠΫϧΛ࣋ͭίϯϙʔωϯτͰ
$PSPVUJOF4DPQFΛ࣮͢Δ w࣮ߦڥͷઃఆͱΩϟϯηϧॲཧΛ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔͱ wίϧʔνϯΛ։࢝͢Δؔ wMBVODIؔΓΛ࣋ͨͳ͍ wBTZODؔΓΛ࣋ͭ
w$PSPVUJOF4DPQFͷ֦ுؔ wͨ͠ϥϜμࣜΛίϧʔνϯͱ͠ ͯىಈ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ wεϨουΛϒϩοΩϯά͢Δ͜ͱͳ͘ॲཧΛఀ ࢭͰ͖Δؔ wίʔϧόοΫͷΑ͏ʹ͋ͱͰఀࢭҐஔ͔Β࠶։
Ͱ͖Δ wTVTQFOEؔଞͷTVTQFOE͔ؔΒͷΈ ݺͼग़͕͠Ͱ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ wεϨουΛϒϩοΩϯά͢Δ͜ͱͳ͘ॲཧΛఀ ࢭͰ͖Δؔ wίʔϧόοΫͷΑ͏ʹ͋ͱͰఀࢭҐஔ͔Β࠶։
Ͱ͖Δ wTVTQFOEؔଞͷTVTQFOE͔ؔΒͷΈ ݺͼग़͕͠Ͱ͖Δ wίϧʔνϯϏϧμʔʹΘͨ͢ϥϜμࣜ TVTQFOEϥϜμʹͳ͍ͬͯΔɻ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ͜͜ͰॲཧΛதஅ͠ɺ࠶։࣌ʹ1SPpMFΛฦؔ͢ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ίϧʔνϯͷ࣮ߦڥΛΓସ͑ΔTVTVQFOEؔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ίϧʔνϯͷ࣮ߦڥΛΓସ͑ΔTVTVQFOEؔ ͜͜ͰϫʔΧʔεϨουͰಈ͘ઃఆΛ͍ͯ͠Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } "1*ϦΫΤετɻϒϩοΩϯά͢Δॲཧ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ίϧʔνϯΛىಈ %JTQBUDIFST.BJO KPCͱ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠ೖ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠ೖ 6*εϨουͰಈ࡞ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠ೖ 6*εϨουͰಈ࡞ ᶅ7JFXʹө -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠ೖ 6*εϨουͰಈ࡞ ᶅ7JFXʹө ྫ֎࣌ͬͪ͜ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } ᶃݺͼग़ͯ͠தஅ͢Δ ϫʔΧʔεϨουͰಈ࡞ ᶄ࠶։ͯ͠ೖ 6*εϨουͰಈ࡞ ᶅ7JFXʹө ྫ֎࣌ͬͪ͜ Ωϟϯηϧ είʔϓ͕ऴྃͨ͠ ͬͪ͜ -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) view.showProfile(profile) } catch(e:CancellationException) { // no op } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ྻʹॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) { requestTicketCount(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) val ticketCount = getTicketCount(id) view.showProfile(profile, ticketCount) } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ྻʹॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) { requestProfile(id) } suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) { requestTicketCount(id) } fun showProfile(id: Int) { launch { try { val profile = getProfile(id) val ticketCount = getTicketCount(id) view.showProfile(profile, ticketCount) } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ඇಉظॲཧΛॻ͘ wXJUI$POUFYUؔΛͬͯίϧʔνϯͷ࣮ߦ ڥΛΓସ͑Δ wྻͰ࣮ߦ͢Δ߹୯७ʹฒΔ͚ͩͰྑ͍
wΤϥʔͱΩϟϯηϧUSZDBUDIͰॲཧ͢Δ wඇಉظॲཧͷ΄ͱΜͲ͜ΕͰରԠͰ͖Δͣ -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } DPSPVUJOF4DPQFؔΛͬͯ ࢠείʔϓΛ։࢝͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } BTZODؔͰίϧʔνϯΛෳىಈ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻʹॻ͘ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } ͪ߹ΘͤΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQF͕ؔͳ͔ͬͨΒ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQF͕ؔͳ͔ͬͨΒ suspend fun getProfile(id:
Int): Profile = withContext(Dispatchers.IO) {…} suspend fun getTicketCount(id: Int): Int = withContext(Dispatchers.IO) {…} fun showProfile(id: Int) { launch { try { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑʹ͓͚ΔHPUP void save(int data,
File file) { try (FileOutputStream out = new FileOutputStream(file);) { new Thread() { @Override public void run() { try { out.write(data); } catch (IOException e) { // error } } }.start(); } catch (IOException e) { // error } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑʹ͓͚ΔHPUP void save(int data,
File file) { try (FileOutputStream out = new FileOutputStream(file);) { new Thread() { @Override public void run() { try { out.write(data); } catch (IOException e) { // error } } }.start(); } catch (IOException e) { // error } } USZXJUISFTPVSDFTͰϑΝΠϧΛ։͘ -
©2018 Lang-8 Inc. ALL Rights Reserved. void save(int data, File
file) { try (FileOutputStream out = new FileOutputStream(file);) { new Thread() { @Override public void run() { try { out.write(data); } catch (IOException e) { // error } } }.start(); } catch (IOException e) { // error } } ฒߦੑʹ͓͚ΔHPUP USZXJUISFTPVSDFTͷதͰεϨου Λىಈͯ͠PVUʹ৮ͬͯඞͣDMPTFࡁ ΈͳͷͰΤϥʔʹͳΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑΛߏԽ͢Δ fun showProfile(id: Int)
{ launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒߦੑΛߏԽ͢Δ fun showProfile(id: Int)
{ launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } 1BSFOU4DPQF $IJME4DPQF MBVODI BTZOD BTZOD -
©2018 Lang-8 Inc. ALL Rights Reserved. είʔϓͱྫ֎ 1BSFOU4DPQF $IJME4DPQF MBVODI
BTZOD BTZOD είʔϓͷฒߦذͷ͍ͣΕ͔͕ΤϥʔʹͳΔͱ είʔϓશମ͕Τϥʔ Ωϟϯηϧ ʹͳΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQFؔ fun showProfile(id: Int)
{ launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } UISPX&YDFQUJPO -
©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQFؔ fun showProfile(id: Int)
{ launch { try { coroutineScope { val profile = async { getProfile(id) } val tickets = async { getTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } } catch (e: Exception) { view.showError(e) } } } ࢠείʔϓ͕ऴྃ͠ྫ֎͕ඈΜͰ͘Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. DPSPVUJOF4DPQF͕ؔͳ͔ͬͨΒ fun showProfile(id: Int)
{ launch { try { val profile = async { requestProfile(id) } val tickets = async { requestTicket(profile.id) } view.showProfile(profile.await(), tickets.await()) } catch (e: Exception) { view.showError(e) } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. 1BSFOU4DPQF MBVODI BTZOD BTZOD
DPSPVUJOF4DPQF͕ؔͳ͔ͬͨΒ -
©2018 Lang-8 Inc. ALL Rights Reserved. 1BSFOU4DPQF MBVODI BTZOD BTZOD
DPSPVUJOF4DPQF͕ؔͳ͔ͬͨΒ -
©2018 Lang-8 Inc. ALL Rights Reserved. ฒྻॲཧΛॻ͘ wίϧʔνϯͷதͰίϧʔνϯ ذ Λىಈ͢Δ
߹DPSPVUJOF4DPQFؔΛ͏ είʔϓͷ ֊ߏΛߟ͑Δ wMBVODIBTZODΛωετͯ͠Δ࣌ҙ wذ͕ΤϥʔʹͳͬͯείʔϓΛܧଓͰ͖Δ TVQFSWJTPS4DPQF͕ؔ͋Δ ࠓճলུ IUUQTLPUMJOHJUIVCJPLPUMJOYDPSPVUJOFTLPUMJOYDPSPVUJOFTDPSFLPUMJOYDPSPVUJOFTTVQFSWJTPSTDPQFIUNM -
©2018 Lang-8 Inc. ALL Rights Reserved. 3Y+BWBͱͷ౷߹ wLPUMJOYDPSPVUJOFTSYΛ͏ w4JOHMF .BZCF
$PNQMFUBCMFͳͲΛ TVTQFOEؔʹͰ͖Δ wTVTQFOEؔΛ4JOHMF .BZCF $PNQMFUBCMFͳͲͰ͖Δ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTUSFFNBTUFSSFBDUJWFLPUMJOYDPSPVUJOFTSY -
©2018 Lang-8 Inc. ALL Rights Reserved. 4JOHMFΛTVTQFOEؔʹ͢Δ interface RemoteService {
@GET("/profile") fun getProfile(id: Int): Single<Profile> } -
©2018 Lang-8 Inc. ALL Rights Reserved. 4JOHMFΛTVTQFOEؔʹ͢Δ interface RemoteService {
@GET("/profile") fun getProfile(id: Int): Single<Profile> } val profile = withContext { service.getProfile(id).await() } 4JOHMFͷ֦ுؔʹ BXBJU ͕Ճ͞Ε͍ͯΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔΛ4JOHMFʹ͢Δ fun getProfile(id: Int):
Single<Profile> { return rxSingle { Profile(id) } } TVTQFOEϥϜμΛड͚औͬͯ݁ՌΛ 4JOHMFͰฦؔ͢ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔΛ4JOHMFʹ͢Δ fun getProfile(id: Int):
Single<Profile> { return rxSingle { Profile(id) } } TVTQFOEϥϜμΛड͚औͬͯ݁ՌΛ 4JOHMFͰฦؔ͢ ΤϥʔʹͳͬͯείʔϓΛ Ωϟϯηϧ͠ͳ͍ -
©2018 Lang-8 Inc. ALL Rights Reserved. 3Y+BWBXJUI$PSPVUJPOF w LPUMJOYDPSPVUJOFTSYͰ૬ޓͷΠϯςάϨʔγϣ ϯ͕Մೳ
w ྫ͑ෳࡶʹ߹͕ඞཁͳՕॴͳͲίϧʔνϯͰϑϥο τʹॻ͍ͯ࠷ޙʹ4JOHMFʹ͢Δͱ͔͕͍͍͔ w ରཱ͢ΔͷͰͳ͍ͷͰ͍͍ײ͡ʹ͏ͷ͕ྑ͍ ͓͢ ͢Ί4JOHMF .BZCF $PNQMFUBCMFͷஔ͖͑ -
©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,
param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,
param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } EP8PSL͕ؔTVTQFOEؔʹͳΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,
param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } ΦʔόʔϥΠυͰ͖Δɻ σϑΥϧτ%JTQBUDIFST%FGBVMU -
©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,
param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } Γ͕8PSLFSͱҟͳΔɻ PVUQVU%BUBΛ໌ࣔతʹ͢ඞཁ͕͋Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. 8PSL.BOBHFSͱ$PSPVUJOF8PSLFS class MyWorker(context: Context,
param: WorkerParameters) : CoroutineWorker(context, param) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Payload { return Payload(Result.SUCCESS) } } Γ͕8PSLFSͱҟͳΔɻ PVUQVU%BUBΛ໌ࣔతʹ͢ඞཁ͕͋Δ public Payload(@NonNull Result result, @NonNull Data output) { mResult = result; mOutput = output; } -
©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ @Dao interface UserDao
{ @Insert suspend fun insert(user: User) @Update suspend fun update(user: User) @Query("SELECT * FROM user") suspend fun getAllUsers(): List<User> @Delete suspend fun delete(user: User) } -
©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ val job =
Job() val scope = CoroutineScope(Dispatchers.Main + job) scope.launch { try { val dao = db.userDao() dao.insert(User(firstName = "Jack", lastName = "Jill")) // do something } catch (e: java.lang.Exception) { // insert error } } -
©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ val job =
Job() val scope = CoroutineScope(Dispatchers.Main + job) scope.launch { try { val dao = db.userDao() dao.insert(User(firstName = "Jack", lastName = "Jill")) // do something } catch (e: java.lang.Exception) { // insert error } } ෦ͰXJUI$POUFYU͕ؔݺͼग़͞ΕΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. 3PPNͱTVTQFOEؔ val job =
Job() val scope = CoroutineScope(Dispatchers.Main + job) scope.launch { try { val dao = db.userDao() dao.insert(User(firstName = "Jack", lastName = "Jill")) // do something } catch (e: java.lang.Exception) { // insert error } } ෦ͰXJUI$POUFYU͕ؔݺͼग़͞ΕΔ 3PPNͷ2VFSZ&YFDVUPSΛ %JTQBUDIFSʹ͍ͯ͠Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ2VFSZ&YFDVUPS val db =
Room.databaseBuilder( applicationContext, AppDatabase::class.java, "database-name" ).apply { val dispatcher = Dispatchers.IO if (dispatcher is ExecutorCoroutineDispatcher) { setQueryExecutor(dispatcher.executor) } }.build() -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ2VFSZ&YFDVUPS val db =
Room.databaseBuilder( applicationContext, AppDatabase::class.java, "database-name" ).apply { val dispatcher = Dispatchers.IO if (dispatcher is ExecutorCoroutineDispatcher) { setQueryExecutor(dispatcher.executor) } }.build() 3PPN#VJMEFSͰ&YFDVUPSΛઃఆͰ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊ w ίϧʔνϯলϝϞϦɺॻ͖͍͢ ಡΈ͍͢
ɺϥΠϒϥϦͱ ͯܰ͠ྔ w $PSPVUJOF4DPQFͱϥΠϑαΠΫϧΛ࣋ͭίϯϙʔωϯτΛ Έ߹Θͤͯ͏ w ୯७ͳඇಉظॲཧXJUI$POUFYUؔͰେΧόʔͰ͖Δ w ฒྻͰίϧʔνϯΛىಈ͢Δ߹είʔϓͷ֊Λҙࣝ͢Δඞ ཁ͕͋Δɻ w 3Yͱରཱ͠ͳ͍ɻҰ෦ͷॲཧ͕ஔ͖͕͑Ͱ͖Δ͔ w +FUQBDLͰଓʑαϙʔτ͕ਐΜͰಋೖ͘͢͠ͳ͖͍ͬͯͯΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL
Rights Reserved. ͋Γ͕ͱ͏͍͟͝·ͨ͠