Share
Embed
Kotlin コルーチンを 理解しよう
- コルーチンとはなにか
- Kotlinはどのようにコルーチンを実現しているのか
- Kotlin コルーチンの基本的な使い方
yagi
August 25, 2018
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
178 slides
-
©2018 Lang-8 Inc. ALL Rights Reserved. wീढ़ w!TZTZBHJ w$50PG w"OESPJE&OHJOFFS
"CPVU.F Jetpack Handbook ʲC94৽ץʳ ʮୈ6ষ WorkManagerͱόοΫάϥϯυλεΫʯ ॻ͖·ͨ͠ -
©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL
Rights Reserved. https://hinative.com 8FBSFIJSJOH -
©2018 Lang-8 Inc. ALL Rights Reserved. w $PCPMίϯύΠϥΛܰྔʹ࣮ ͢ΔͨΊʹίϧʔνϯͷ֓೦ Λಋೖͨ͠
w ͋Δ݅ԼͰϓϩάϥϜΛ ͢Δͱಠཱͯͦ͠ΕͧΕಈ࡞Ͱ ͖Δͱ͍͏ΞΠσΞ w DPSPVUJOFɺͭ·Γରͷϧʔ νϯͱ͍͏ҙຯɻ͓ޓ͍Λݺͼ ग़੍ͯ͠ޚͰ͖Δؔ ϝϧϰΟϯɾίϯΣΠ ͷͷจ͕ॳग़ IUUQNFMDPOXBZDPN)PNFQEGDPNQJMFSQEG -
©2018 Lang-8 Inc. ALL Rights Reserved. w "DPSPVUJOFJTBOJOTUBODFPGTVTQFOEBCMF DPNQVUBUJPO*UJTDPODFQUVBMMZTJNJMBSUPB UISFBE
JOUIFTFOTFUIBUJUUBLFTBCMPDLPG DPEFUPSVOBOEIBTBTJNJMBSMJGFDZDMFJU JTDSFBUFEBOETUBSUFE CVUJUJTOPUCPVOEUP BOZQBSUJDVMBSUISFBE*UNBZTVTQFOEJUT FYFDVUJPOJOPOFUISFBEBOESFTVNFJOBOPUIFS POF.PSFPWFS MJLFBGVUVSFPSQSPNJTF JU NBZDPNQMFUFXJUITPNFSFTVMUPSFYDFQUJPO $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ -
©2018 Lang-8 Inc. ALL Rights Reserved. w ίϧʔνϯɺҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯεͰ͢ɻͦ Ε֓೦తʹεϨουʹࣅ͍ͯ·͢ɻͭ·Γɺ࣮ߦ͢Δίʔ υϒϩοΫΛ࣋ͪɺಉ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞͞Ε
ىಈ͞Ε·͕͢ɺಛఆͷεϨουʹଋറ͞Ε͍ͯ·ͤΜɻ͋ ΔεϨουͰ࣮ߦΛதஅ͠ɺผͷεϨουͰ࠶։͢Δ͜ͱ͕ ͋Γ·͢ɻ͞Βʹɺ'VUVSF1SPNJTTͷΑ͏ʹɺԿΒ͔ ͷ݁Ռྫ֎͕͋Δ߹͕͋Γ·͢ɻ $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ -
©2018 Lang-8 Inc. ALL Rights Reserved. w ίϧʔνϯɺҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯεͰ͢ɻͦ Ε֓೦తʹεϨουʹࣅ͍ͯ·͢ɻͭ·Γɺ࣮ߦ͢Δίʔ υϒϩοΫΛ࣋ͪɺಉ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞͞Ε
ىಈ͞Ε·͕͢ɺಛఆͷεϨουʹଋറ͞Ε͍ͯ·ͤΜɻ͋ ΔεϨουͰ࣮ߦΛதஅ͠ɺผͷεϨουͰ࠶։͢Δ͜ͱ͕ ͋Γ·͢ɻ͞Βʹɺ'VUVSF1SPNJTTͷΑ͏ʹɺԿΒ͔ ͷ݁Ռྫ֎͕͋Δ߹͕͋Γ·͢ɻ $PSPVUJOFTGPS,PUMJOʹΑΔͱʜ IUUQTHJUIVCDPN,PUMJOLPUMJODPSPVUJOFTCMPCNBTUFSLPUMJODPSPVUJOFTJOGPSNBMNEUFSNJOPMPHZ -
©2018 Lang-8 Inc. ALL Rights Reserved. wαϒϧʔνϯͱҟͳΓɺঢ়ଶཧΛ ҙࣝͤͣʹߦ͑ΔͨΊɺڠௐతॲཧɺ ΠςϨʔλɺແݶϦετɺύΠϓͳ Ͳɺܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰
қʹهड़Ͱ͖Δɻ 8JLJQFEJBʹΑΔͱʜ IUUQTKBXJLJQFEJBPSHXJLJ&#&"#&#$&&# -
©2018 Lang-8 Inc. ALL Rights Reserved. wαϒϧʔνϯͱҟͳΓɺঢ়ଶཧΛ ҙࣝͤͣʹߦ͑ΔͨΊɺڠௐతॲཧɺ ΠςϨʔλɺແݶϦετɺύΠϓͳ Ͳɺܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰
қʹهड़Ͱ͖Δɻ 8JLJQFEJBʹΑΔͱʜ IUUQTKBXJLJQFEJBPSHXJLJ&#&"#&#$&&# -
©2018 Lang-8 Inc. ALL Rights Reserved. wҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε wεϨουͷΑ͏ʹ࣮ߦ͢ΔίʔυϒϩοΫΛ࣋ͪɺಉ ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞͞Εىಈ͞ΕΔ͕ɺ ಛఆͷεϨουʹଋറ͞Εͳ͍
w'VUVSF1SPNJTTͷΑ͏ʹɺԿΒ͔ͷ݁Ռྫ֎ ͕͋Δ߹͕͋Δ wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖Δ ίϧʔνϯͱʜ -
©2018 Lang-8 Inc. ALL Rights Reserved. Ұ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε fun loadProfile(id: Int)
{ val profile = getProfile(id) showProfile(profile) } αϒϧʔνϯ ίϧʔνϯ ։͔࢝ΒϦλʔϯ·Ͱ͕Ұ ͭͷॲཧ୯Ґ தஅ ࠶։ ॲཧͷ్தͰҰ࣌ఀࢭ ͕Մೳ launch { val profile = getProfile(id).await() showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. εϨουͱίϧʔνϯ Thread { val
profile = getProfile(token) showProfile(profile) }.start() εϨου ίϧʔνϯ ίʔυϒϩοΫͱϥΠϑα ΠΫϧΛ࣋ͪɺ࡞ͱىಈ ͕͞ΕΔ εϨουʹࣅ͍ͯΔ͕ ಛఆͷεϨουʹଋറ ͞Εͳ͍ launch { val profile = getProfile(token).await() showProfile(profile) } ͲͷεϨουͰ࣮ߦ͞ΕΔ͔ίϧʔνϯࣗؾʹ͠ͳ͍ -
©2018 Lang-8 Inc. ALL Rights Reserved. 'VUVSF1SPNJTTͷΑ͏ʹΛฦ͢߹͕͋Δ fun getProfile(id: Int)
= async { //… return profile } launch { val profile = getProfile(id).await() showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. 'VUVSF1SPNJTTͷΑ͏ʹΛฦ͢߹͕͋Δ Λฦ͢ίϧʔνϯ fun getProfile(id:
Int) = async { //… return profile } launch { val profile = getProfile(id).await() showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. 'VUVSF1SPNJTTͷΑ͏ʹΛฦ͢߹͕͋Δ fun getProfile(id: Int)
= async { //… return profile } launch { val profile = getProfile(id).await() showProfile(profile) } ผͷίϧʔνϯΛىಈͯࣗ͠தஅ ͠ɺ݁ՌΛड͚औͬͯ࠶։͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun loadProfile(id: Int)
{ val profile = getProfile(id) showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ ஞ࣮࣍ߦ fun loadProfile(id:
Int) { val profile = getProfile(id) showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ ௨৴ॲཧ%#ΞΫηεɺॏ͍ͨܭࢉͳͲΛ ߦ͏߹ɺϒϩοΩϯάͯ͠͠·͏ɻ fun
loadProfile(id: Int) { val profile = getProfile(id) showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { val profile = getProfile(id) showProfile(profile) } ίʔϧόοΫΛड͚औΔܗʹͯ͠ʜ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ϒϩοΩϯά͠ͳ͍ܗʹ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ؔ͜͜ͰҰதஅ͍ͯ͠Δʂ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ࠶։͍ͯ͠Δʜ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ࣋ͭϓϩάϥϜͱ fun getProfile(id: Int,
f: (Profile) -> Unit) fun loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ܧଓঢ়گΛ࣋ͭϓϩάϥϜͬ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)
{ val profile = getProfile(id) showProfile(profile) } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)
{ val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)
{ val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ HFU1SPpMF͕ؔฦ͢ίϧʔν ϯʹ੍ޚΛࣗ͠தஅ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)
{ val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ HFU1SPpMF͕ؔฦ͢ίϧʔν ϯผͷεϨουͰಈ࡞͍ͯ͠ Δ͔͠Εͳ͍ͯ͠͠ͳ͍͔ ͠Εͳ͍ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ fun loadProfile(id: Int)
{ val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ HFU1SPpMF͕ؔྃͨ͠Β Λड͚औΔͱ͜Ζ͔Β࠶։ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گΛ༰қʹॻ͘ ίϧʔνϯ BTZODBXBJU fun
loadProfile(id: Int) { getProfile(token) { profile -> showProgile(profile) } } ίʔϧόοΫελΠϧ fun loadProfile(id: Int) { val profile = getProfile(id).await() showProfile(profile) } ˞ٖࣅίʔυͰ͢ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گ͕૿͑ͯ fun loadProfile(id: Int)
{ getProfile(id) { profile -> getReport(profile.id) { report -> //.. } } } } } } ίʔϧόοΫελΠϧ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓঢ়گ͕૿͑ͯ ίϧʔνϯ BTZODBXBJU fun
loadProfile(id: Int) { val profile = getProfile(id).await() val report = getReport(profile.id).await() val a = getA().await() val b = getB().await() val c = getC().await() //… } ˞ٖࣅίʔυͰ͢ -
©2018 Lang-8 Inc. ALL Rights Reserved. wδΣωϨʔλɺετϦʔϜ wνϟϯωϧ wΞΫλʔϞσϧ wύΠϓϥΠϯ
wFUDʜ ܧଓঢ়گΛ؆୯ʹॻ͚Δ͜ͱͰ ָ࣮͕ʹͳΔͷͨͪ -
©2018 Lang-8 Inc. ALL Rights Reserved. wҰ࣌ఀࢭՄೳͳܭࢉͷΠϯελϯε wεϨουͷΑ͏ʹ࣮ߦ͢ΔίʔυϒϩοΫΛ࣋ͪɺಉ ༷ͷϥΠϑαΠΫϧΛ࣋ͪɺ࡞͞Εىಈ͞ΕΔ͕ɺ ಛఆͷεϨουʹଋറ͞Εͳ͍
w'VUVSF1SPNJTTͷΑ͏ʹɺԿΒ͔ͷ݁Ռྫ֎ ͕͋Δ߹͕͋Δ wܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖Δ ίϧʔνϯͱʜ -
©2018 Lang-8 Inc. ALL Rights Reserved. ,PUMJOͲͷΑ͏ʹ ίϧʔνϯΛ࣮ݱ͍ͯ͠Δͷ͔ʁ fun loadProfile(id:
Int) { val profile = getProfile(id).await() showProfile(profile) } )PX -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {
val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {
val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ͜͜Ͱதஅ͢Δͱ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {
val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ̎ͭͷঢ়ଶʹղͰ͖Δ ̎ͭͷঢ়ଶʹղͰ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ fun simpleCoroutine() {
val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ̎ͭͷঢ়ଶʹղͰ͖Δ ԣஅతʹར༻͢Δม -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛεςʔτϚγϯʹ͢Δ class SimpleCoroutine {
var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTimeMillis() println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.currentTimeMillis() - start}”) label++ } } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var
label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var
label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } ̎ͭͷঢ়ଶΛ࣋ͭ ̎ͭͷঢ়ଶΛ࣋ͭ -
©2018 Lang-8 Inc. ALL Rights Reserved. ݱࡏͷঢ়ଶΛ࣋ͭ ݱࡏͷঢ়ଶΛද͢ ass SimpleCoroutine
{ var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var
label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } ݱࡏΛมԽͤ͞Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ڞ௨ͯ͠͏ม ass SimpleCoroutine {
var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ॲཧΛ࠶։͢Δؔ ass SimpleCoroutine {
var label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var
label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. fun delay(delayTime: Long, coroutine:
SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } -
©2018 Lang-8 Inc. ALL Rights Reserved. fun delay(delayTime: Long, coroutine:
SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } ࠶։ͷͨΊʹ4JNQMF$PSPVUJOF Λड͚औ͓ͬͯ͘ -
©2018 Lang-8 Inc. ALL Rights Reserved. ࢦఆ͞Εͨ࣌ؒεϦʔϓͯ͠ 4JNQMF$PSPVUJOFΛ࠶։͢Δ fun delay(delayTime:
Long, coroutine: SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } -
©2018 Lang-8 Inc. ALL Rights Reserved. ass SimpleCoroutine { var
label = 0 var start = 0L fun resume() { when (label) { 0 -> { start = System.currentTi println(“start") label++ delay(1000, this) } 1 -> { println("end ${System.cu label++ } } } -
©2018 Lang-8 Inc. ALL Rights Reserved. val simpleCoroutine = SimpleCoroutine()
simpleCoroutine.resume() fun simpleCoroutine() { val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ίϧʔνϯ εςʔτϚγϯ -
©2018 Lang-8 Inc. ALL Rights Reserved. val simpleCoroutine = SimpleCoroutine()
simpleCoroutine.resume() fun simpleCoroutine() { val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ίϧʔνϯ εςʔτϚγϯ TUBSU FOE TUBSU FOE -
©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯΛϚʔΩϯά͢ΔͨΊʹTVTQFOEम ০ࢠΛಋೖ wϥϜμࣜʹTVTQFOEम০ࢠΛ༩͢Δͱίϧʔ νϯຊମΛද͢
TVTQFOEϥϜμ wؔʹ༩͢Δͱதஅ͢ΔॴΛද͢ɻͦͷؔΛ TVTQFOEؔͱݺͿ wTVTQFOEؔTVTQFOEؔϥϜμ͔Β͔͠ ݺͼग़ͤͳ͍ TVTQFOEम০ࢠ -
©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯࣗΛܧଓΦϒδΣΫτʹ͢Δ wTVTQFOEؔΛܧଓ͠ελΠϧ $POUJOVBUJPO1BTTJOH4UZMF $14
ʹม͢Δ wTVTQFOEؔͰܧଓΦϒδΣΫτΛऔΓ ग़ͯ͠ҙʹ࠶։Ͱ͖ΔΑ͏ʹ͢Δ ܧଓ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ$14 fun delay(delayTime: Long,
coroutine: SimpleCoroutine) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ$14 suspend fun delay(delayTime:
Long) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ$14 suspend fun delay(delayTime:
Long) { Thread { Thread.sleep(delayTime) coroutine.resume() }.start() } // c: Continuation<T> ίϯύΠϧ࣌ʹࣗಈͰ ܧଓΠϯλϑΣʔεͷ Ҿ͕૿͑Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ$14 suspend fun delay(delayTime:
Long) { // c: Continuation<T> suspendCoroutine<Unit> { c -> Thread { Thread.sleep(delayTime) c.resume(Unit) }.start() } } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ$14 suspend fun delay(delayTime:
Long) { // c: Continuation<T> suspendCoroutine<Unit> { c -> Thread { Thread.sleep(delayTime) c.resume(Unit) }.start() } } ܧଓΠϯλϑΣʔεΛऔΓग़͢ ͨΊʹ༻ҙ͞Ε͍ͯΔؔ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEؔͱ$14 suspend fun delay(delayTime:
Long) { // c: Continuation<T> suspendCoroutine<Unit> { c -> Thread { Thread.sleep(delayTime) c.resume(Unit) }.start() } } ܧଓΠϯλϑΣʔεΛͬͯ࠶։ ॲཧΛߦ͏ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } } TVTQFOEϥϜμʹΑͬͯίϧʔνϯͷ ൣғ͕ܾ·Δɻ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } TVTQFOEϥϜμͷ֦ு͕͍͔ؔͭ͘༻ ҙ͞Ε͍ͯΔɻTUBSU$PSPVUJOFؔʹΑͬ ͯίϧʔνϯΛ։࢝Ͱ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } TUBSU$PSPVUJOFؔ$POUJOVBUJPO ΛҾʹऔΔɻίϧʔνϯ͕ྃͨ͠Βݺ ͼग़͞ΕΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } ʹͳΔͨΊศ্ٓ༻ҙ ͨ͠Ϋϥε object NoOpCompletion<T> : Continuation<T> { override val context: CoroutineContext = EmptyCoroutineContext override fun resume(value: T) { // no op } override fun resumeWithException(exception: Throwable) { // no op } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } -
©2018 Lang-8 Inc. ALL Rights Reserved. TVTQFOEϥϜμͱίϧʔνϯ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } TUBSU FOE -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯΛ࡞ΔͷΊΜͲ͍͘͞ fun simpleCoroutine() {
val f: suspend () -> Unit = { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } f.startCoroutine(NoOpCompletion) } -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯϏϧμʔ fun simpleCoroutine() {
launch { val start = System.currentTimeMillis() println(“start") delay(1000) println("end ${System.currentTimeMillis() - start}”) } } ͦΕͧΕͷੑ࣭ΛͭίϧʔνϯϏϧμʔ ͕ίϧʔνϯඪ४ϥΠϒϥϦͰఏڙ͞ΕΔ ͷͰීஈͦͪΒΛ͏͜ͱʹͳΔɻ -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓΠϯλʔηϓλʔͱ ࣮ߦεϨου launch(CommonPool) {
// … } launch(UI) { // … } launch(JavaFx) { // … } launch(Swing) { // … } async(CommonPool) { // … } -
©2018 Lang-8 Inc. ALL Rights Reserved. ܧଓΠϯλʔηϓλʔͱ ࣮ߦεϨου launch(CommonPool) {
// … } launch(UI) { // … } launch(JavaFx) { // … } launch(Swing) { // … } async(CommonPool) { // … } ίϧʔνϯϏϧμʔʹܧଓΠϯ λʔηϓλʔΛ࣮ͯ͠ߦεϨο υΛίϯτϩʔϧͰ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. wίϧʔνϯΛεςʔτϚγϯʹม͠ ͍ͯΔ wมͷͨΊʹTVTQFOEम০ࢠͱܧ ଓΛಋೖͨ͠
wܧଓΠϯλʔηϓλʔʹΑΓɺ࣮ߦͷ εϨουΛݶఆ͠ͳ͍ ,PUMJOͲͷΑ͏ʹ ίϧʔνϯΛ+BWBͰ࣮ݱ͍ͯ͠Δͷ͔ʁ -
©2018 Lang-8 Inc. ALL Rights Reserved. ֤छػೳϥΠϒϥϦͰఏڙ wDPSF SFBDUJWF OBUJWF
VJ KTͳͲ ͷϞδϡʔϧʹ͔Ε͍ͯΔ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT -
©2018 Lang-8 Inc. ALL Rights Reserved. ಛఆͷڥ͚ͷϥΠϒϥϦ͋Δ w"OESPJE +BWB'Y 4XJOH͚͕༻ҙ͞
Ε͍ͯΔ ܧଓΠϯλʔηϓλʔͷ࣮ IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFTUSFFNBTUFSVJ -
©2018 Lang-8 Inc. ALL Rights Reserved. ڥߏங kotlin { experimental
{ coroutines 'enable' } } dependencies { implementation”org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.60" implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0" // platformʹ߹ΘͤͯՃ implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0" } BQQCVJMEHSBEMF -
©2018 Lang-8 Inc. ALL Rights Reserved. ڥߏங kotlin { experimental
{ coroutines 'enable' } } dependencies { implementation”org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.60" implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0" // platformʹ߹ΘͤͯՃ implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0" } BQQCVJMEHSBEMF ແͯ͘ಈ͚͘Ͳ*%&্ͱ ͔Ͱܯࠂ͕ͰΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val
start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } -
©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val
start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ίϧʔνϯϏϧμʔ -
©2018 Lang-8 Inc. ALL Rights Reserved. w݁ՌΛ࣋ͨͳ͍ίϧʔνϯΛ࡞͢Δί ϧʔνϯϏϧμʔؔ MBVODIؔ public
fun launch( context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job -
©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ public fun launch(
context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯຊମɻ$PSPVUJOF4DPQFͷ֦ு TVTQFOEϥϜμ -
©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ public fun launch(
context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯͷ࣮ߦίϯςΩετɺڞ༗σʔ λܧଓΠϯλʔηϓλʔͳͲΛ͜͜Ͱࢦ ఆͰ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ public fun launch(
context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job -
©2018 Lang-8 Inc. ALL Rights Reserved. %FGBVMU%JTQBUDIFS public actual val
DefaultDispatcher: CoroutineDispatcher = if (useCoroutinesScheduler) ExperimentalCoroutineDispatcher() else CommonPool -
©2018 Lang-8 Inc. ALL Rights Reserved. %FGBVMU%JTQBUDIFS tcher() else CommonPool
εϨουϓʔϧΛͬͯίϧʔνϯΛ࣮ߦ͢Δ ܧଓΠϯλʔηϓλʔɻ࣮ߦڥͷ$16ίΞ ʹԠͯ͡εϨουมԽ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ public fun launch(
context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯͷ։࢝ํ๏Λࢦఆ͢ΔɻσϑΥ ϧτͰଈ࠲ʹ։࢝͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ public fun launch(
context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ىಈͨ͠ίϧʔνϯͷྃΛͪ߹Θͤͨ ΓΩϟϯηϧͰ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ public fun launch(
context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job +PCΛઃఆ͢Δɻෳͷίϧʔνϯ ΛҰׅͰDBODFM͍ͨ࣌͠ͳͲʹ͏ -
©2018 Lang-8 Inc. ALL Rights Reserved. MBVODIؔ public fun launch(
context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job ίϧʔνϯͷྃΛίʔϧόοΫͰड͚औ Γ͍ͨ߹ʹࢦఆ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val
start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } -
©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val
start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ඪ४ϥΠϒϥϦ͕ఏڙ͢Δ TVTQFOEؔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ࠷ॳͷίϧʔνϯ launch { val
start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } TUBSU FOE -
©2018 Lang-8 Inc. ALL Rights Reserved. ίϧʔνϯͱ࣮ߦεϨου launch { val
start = System.currentTimeMillis() println("start") delay(1000) println("end ${System.currentTimeMillis() - start}") } ͲͪΒͷঢ়ଶεϨουϓʔ ϧͰ࣮ߦ͞ΕΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch { prograssBar.isVisible
= true delay(1000) prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch { prograssBar.isVisible
= true delay(1000) prograssBar.isVisible = false } %FGBVMU%JTQBUDIFSεϨουϓʔ ϧͰίϧʔνϯΛ࣮ߦ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch(UI) { prograssBar.isVisible
= true delay(1000) prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch(UI) { prograssBar.isVisible
= true delay(1000) prograssBar.isVisible = false } LPUMJOYDPSPVUJOFTBOESPJE͕ఏڙ͢Δ ܧଓΠϯλʔηϓλʔɻৗʹ6*εϨουͰ࠶։ ͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ྫ͑6*Λ৮ΔΑ͏ͳίϧʔνϯͩͱʜ launch(UI) { prograssBar.isVisible
= true delay(1000) prograssBar.isVisible = false } 6*εϨουͰ࣮ߦ 6*εϨουͰ࣮ߦ -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU launch(UI) { prograssBar.isVisible
= true delay(1000) prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true delay(1000) prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true delay(1000) prograssBar.isVisible = false } ٖࣅతͳ௨৴ॲཧ -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = loadProfile() nameText.text = profile.name prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = loadProfile() nameText.text = profile.name prograssBar.isVisible = false } ϒϩοΩϯά͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } BTZODؔΛ͏ -
©2018 Lang-8 Inc. ALL Rights Reserved. w݁ՌΛ࣋ͭίϧʔνϯΛ࡞͢Δίϧʔ νϯϏϧμʔؔ BTZODؔ public
fun <T> async( context: CoroutineContext = DefaultDispatcher, start: CoroutineStart = CoroutineStart.DEFAULT, parent: Job? = null, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> T ): Deferred<T> -
©2018 Lang-8 Inc. ALL Rights Reserved. %FGFSSFE5 public interface Deferred<out
T> : Job { //… public suspend fun await(): T //… } தஅͯ݁͠ՌΛͬͯ࠶։͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } ܧଓΛड͚औͬͯॲཧͷྃΛͭ -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { Thread.sleep(1000) return Profile() } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } ඇಉظॲཧ͕ྃ͢Δͱɺ6*εϨο υͰܧଓΛ࠶։͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. BTZODBXBJU class Profile(val name:
String = "Jack") fun loadProfile(): Profile { val profile = http.get(“http:/.”) return profile } launch(UI) { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. wMBVODIؔ݁Ռ͕ແ͍ίϧʔνϯΛɺBTZOD ؔ݁Ռ͕͋ΔίϧʔνϯΛ࡞Δ wσϑΥϧτͰ$PNNPO1PPM͕ΘΕΔͷͰ ඞཁʹԠͯ͡ܧଓΠϯλʔηϓλʔΛηοτ͢
Δ wMBVODIؔͱBTZODؔΛΈ߹Θͤͯ͏ େମ͜ΕͰࣄΓΔ ίϧʔνϯͷجຊతͳ͍ํ -
©2018 Lang-8 Inc. ALL Rights Reserved. جຊܗ launch(UI) { prograssBar.isVisible
= true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } -
©2018 Lang-8 Inc. ALL Rights Reserved. ΤϥʔϋϯυϦϯά launch(UI) { ɹtry{
prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val token = async { getToken() } val profile = async { loadProfile(token.await()) }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val token = async { getToken() } val profile = async { loadProfile(token.await()) }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ͜͜Ͱதஅͤͣ։࢝ ͚͍ͩͯ͠Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val token = async { getToken() } val profile = async { loadProfile(token.await()) }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ͜͜Ͱ݁ՌΛ͍ͬͯΔ -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val profile = async { loadProfile() }.await() val articles = async { loadArticles() }.await() show(profile, articles) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val profile = async { loadProfile() }.await() val articles = async { loadArticles() }.await() show(profile, articles) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ྻͰ࣮ߦ͞Εͯ͠·͏ -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val profile = async { loadProfile() } val articles = async { loadArticles() } show(profile.await(), articles.await()) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦΫΤετΛฒྻͰ࣮ߦ͢Δ launch(UI) { ɹtry{
prograssBar.isVisible = true val profile = async { loadProfile() } val articles = async { loadArticles() } show(profile.await(), articles.await()) prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } ฒྻͰ։͓͍࢝ͯͯ͠ɺͪ߹Θͤ Δͱ͜ΖͰҰؾʹBXBJU͢Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM launch(UI) { ɹtry{
prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: Exception) { showError(e) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM val job =
launch(UI) { ɹtry { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: CancellationException) { // cancel } catch (e: Exception) { showError(e) } } //… job.cancel() -
©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM val job =
launch(UI) { ɹtry { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: CancellationException) { // cancel } catch (e: Exception) { showError(e) } } //… job.cancel() ίϧʔνϯΛىಈ͢Δͱ͖ʹ+PCΛ ड͚औ͓͚ͬͯҙͷλΠϛϯά ͰΩϟϯηϧ͕Ͱ͖Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM val job =
launch(UI) { ɹtry { prograssBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name prograssBar.isVisible = false } catch (e: CancellationException) { // cancel } catch (e: Exception) { showError(e) } } //… job.cancel() Ωϟϯηϧͷྫ֎͕ඈΜͰ͘ΔͷͰ Ωϟον -
©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM "OESPJEͷྫ class MainActivity
: AppCompatActivity() { var job: Job? = null //… fun doSomething(){ job = launch(UI) { … } //… override fun onPause() { job?.cancel() super.onPause() } } -
©2018 Lang-8 Inc. ALL Rights Reserved. $BODFM "OESPJEͷྫ class MainActivity
: AppCompatActivity() { var job: Job? = null //… fun doSomething(){ job = launch(UI) { … } //… override fun onPause() { job?.cancel() super.onPause() } } ϥΠϑαΠΫϧʹ߹Θͤ ͯΩϟϯηϧ -
©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :
AppCompatActivity() { var rootJob: Job? = null //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onResume() { super.onResume() rootJob = Job() } override fun onPause() { rootJob.cancel() super.onPause() } -
©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :
AppCompatActivity() { var rootJob: Job? = null override fun onResume() { super.onResume() rootJob = Job() } //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onPause() { rootJob.cancel() super.onPause() } -
©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :
AppCompatActivity() { var rootJob: Job? = null override fun onResume() { super.onResume() rootJob = Job() } //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onPause() { rootJob.cancel() super.onPause() } -
©2018 Lang-8 Inc. ALL Rights Reserved. ·ͱΊͯ$BODFM class MainActivity :
AppCompatActivity() { var rootJob: Job? = null override fun onResume() { super.onResume() rootJob = Job() } //… fun doSomething(){ launch(UI, parent = rootJob) { … } fun doSomething2(){ launch(UI, parent = rootJob) { … } //… override fun onPause() { rootJob?.cancel() super.onPause() } ·ͱΊͯΩϟϯηϧ -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { try
{ progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false } catch (e: Exception) { showError(e) } } -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)
{ try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)
{ try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } GPSจͱҰॹɻ͜Εί ϧʔνϯͰͳ͍ɻ -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)
{ try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } ޭͨ͠ΒMBVODI͔Β ൈ͚Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)
{ try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } ϦτϥΠ͖͢ΤϥʔͰͳ ͍ͳΒSFQFBUΛൈ͚Δ -
©2018 Lang-8 Inc. ALL Rights Reserved. ϦτϥΠ launch(UI) { repeat(3)
{ try { progressBar.isVisible = true val profile = async { loadProfile() }.await() nameText.text = profile.name progressBar.isVisible = false return@launch } catch (e: Exception) { if (!shouldRetry(e)) { return@repeat } } } showError() } ϦτϥΠ͕શࣦͯഊ͔ͨ͠Ϧτϥ Π͖͢Ͱͳ͍Τϥʔ͕ൃੜ߹ -
©2018 Lang-8 Inc. ALL Rights Reserved. 3FUSPpUΛ%FGFSSFEԽ IUUQTHJUIVCDPN+BLF8IBSUPOSFUSPpULPUMJODPSPVUJOFTBEBQUFS +BLF8IBSUPOɻ$BMM"EBQUFSΛ࣮ͯ͠%FGFSSFE Λฦ͢Α͏ʹ͍ͯ͠Δɻ
@POST("/api/v1/articles/{id}/like") fun like(@Path(“id") id: Long): Deferred<ResponseBody> @DELETE(“/api/v1/articles/{id}/like") fun likeCancel(@Path("id") id: Long): Deferred<ResponseBody> @POST(“/api/v1/users/sign_up”) fun signUp(@Body params: SignUpParameter): Deferred<SignInResponseEntity> @POST("/api/v1/users/sign_in") fun signIn(@Body params: SignInParameter): Deferred<SignInResponseEntity> -
©2018 Lang-8 Inc. ALL Rights Reserved. &WFOU#VTͱ$IBOOFM class EventBus {
private val channel = BroadcastChannel<Any>(1) fun send(event: Any, context: CoroutineContext = DefaultDispatcher) { launch(context) { channel.send(event) } } fun subscribe(): ReceiveChannel<Any> = channel.openSubscription() inline fun <reified T> subscribeToEvent() = subscribe().filter { it is T }.map { it as T } } IUUQTHJTUHJUIVCDPNTWFOKBDPCTBCEEBCDEB -
©2018 Lang-8 Inc. ALL Rights Reserved. &WFOU#VTͱ$IBOOFM val bus: EventBus
by inject() override fun onResume() { super.onResume() launch(UI, parent = rootJob) { bus.subscribeToEvent<LogoutEvent>().consumeEach { // logged out } } } override fun onPause() { rootJob.cancel() super.onPause() } //… bus.send(LogoutEvent) -
©2018 Lang-8 Inc. ALL Rights Reserved. PO"DUJWJUZ3FTVMUΛTVTQFOE launch(UI) { val
result = activityResult(intent) if (result.isOk) { // ok val data: Intent? = result.flatMap() } else { // ng } } IUUQTHJUIVCDPNQEWSJF[FBOESPJEDPSPVUJOFT BOESPJEDPSPVUJOFTͱ͍͏αʔυύʔςΟϥΠϒϥϦ͕ ఏڙɻSFRVFTU1FSNJTTJPO͋ΔΑ -
©2018 Lang-8 Inc. ALL Rights Reserved. w ܧଓঢ়گΛ࣋ͭϓϩάϥϜ͕༰қʹهड़Ͱ͖ͯศར w ,PUMJOίϧʔνϯΛεςʔτϚγϯʹม࣮ͯ͠ݱ͠
͍ͯΔ w BTZODBXBJUͳͲͷ֤ػೳ,PUMJOͷެࣜϥΠϒϥϦ ͕ఏڙ͍ͯ͠Δ w MBVODIؔͱBTZODؔͰେΓΔ w ඞཁʹԠͯࣗ͡ͰίϧʔνϯͷΈΛ࣮Ͱ͖Δɻ͢ ͰʹαʔυύʔςΟϥΠϒϥϦ͕͍͔ͭ͋͘Δ ,PUMJOίϧʔνϯ·ͱΊ -
©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL
Rights Reserved. &OKPZ,PUMJO$PSPVUJOF -
©2018 Lang-8 Inc. ALL Rights Reserved. ©2018 Lang-8 Inc. ALL
Rights Reserved. ͋Γ͕ͱ͏͍͟͝·ͨ͠