Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

RxJava / Coroutines? Taehwan

Slide 3

Slide 3 text

Threadۆ? Coroutines ੉ۆ? ReactivieXۆ? - RxJava/RxKotlin/RxAndroid RxJava৬ Coroutinesਸ ೣԋ ࢎਊೞ۰ݶ? ೣ੿ ౵ӝ > ӝࠄ ѐ֛ਸ ࡅܰѱ ঌইࠁҊ, ઱੄ೡ ղਊਸ ਤ઱۽ ׮ܘ׮ য়ט ঌইࠅ ղਊ

Slide 4

Slide 4 text

Rx੄ ݆ࣻ਷ operatorܳ ׮ܖ૑ ঋח׮. Coroutines੄ ׮নೠ ੽Ӕ ߑߨਸ ׮ܖ૑ח ঋח׮ য়ט ঌইࠁ૑ ঋਸ ղਊ ઱੄ : ӝ؀ೠ ࠗ࠙਷ হਸ ࣻ ੓׮

Slide 5

Slide 5 text

RxJava৬ Coroutinesਸ ঌӝ ੹ী Threadࠗఠ

Slide 6

Slide 6 text

Threadח Processীࢲ ੘সਸ ೞӝ ਤೠ ױਤ Thread { /* Block */ } ഋక۽ ҳഅ Thread? Thread { Thread.sleep(500L) Handler(Looper.getMainLooper()).post { Toast.makeText( this, "lazy Toast!", Toast.LENGTH_SHORT) .show() } }.start()

Slide 7

Slide 7 text

RxJava - Threadܳ ખ ؊ औҊ, ಞೞѱ ॳӝ ਤೠ operator ݽ਺ ഋక - Observer pattern, Iterator pattern, Functional programming Ѿ೤ Coroutines - Assembly languageীࢲ ࢎਊغ঻؍ Routine ѐ֛ - JVM ਤীࢲח Threadܳ ࢎਊ೧ঠ ೠ׮ * JVM (Java virtual machine) JVMীࢲ ز੘ೞ۰ݶ Threadо ೙ਃೞ׮

Slide 8

Slide 8 text

Coroutines?

Slide 9

Slide 9 text

Asynchronous or non-blocking programming is the new reality. Observer pattern ࢎਊਸ ઴ੌ ࣻ ੓חؘ, return ೧઴ ٸө૑ ؀ӝ दఅ׮ RxJava ࠁ׮ ҳܾ ࣻ ੓׮ ॶࣻ۾ য۵׮(Error ୊ܻۄ؍о, ղо ॳח ѱ ݏחо?ۄ؍о) Coroutines? https://kotlinlang.org/docs/reference/coroutines-overview.html

Slide 10

Slide 10 text

ഐ୹ೞח ೣࣻ੄ returnਸ ӝ׮ܻҊ, যڌѱ ӝ׮ܾө? suspend fun loadData(): Item { // load data delay(500) return Item() } @Test fun test() = runBlocking { val item = loadData() } ੉ ਽׹ чਸ о૑Ҋ ୊ܻೡ ࣻ ੓׮.

Slide 11

Slide 11 text

ReactiveX?

Slide 12

Slide 12 text

An API for asynchronous programming with observable streams 3о૑ ഋకܳ Ѿ೤ೠ observable streams - Observable pattern - Iterator pattern - Functional programming Android - RxJava - RxAndroid - RxKotlin ReactiveX? http://reactivex.io/

Slide 13

Slide 13 text

Javaܳ ਤೠ RxJava ۄ੉࠳۞ܻ RxJava 1.x : 2018֙ 12ਘ 31ੌ ੉޷ ઙܐغ঻׮. RxJava 2.x : 2020֙ 12ਘ 31ੌө૑ ࠁউ ಁ஖ ૑ਗೠ׮. RxJava 3.x : RC 5ө૑ աৡ ࢚కۄ ҍ ߓನೡ٠ೞ׮. RxJava https://github.com/ReactiveX/RxJava

Slide 14

Slide 14 text

RxJava੄ ੌࠗ ഛ੢ ೣࣻܳ ઁҕ combineLatest zip withLatestFrom ١… disposableਸ ਤೠ ഛ੢ ೣࣻ ઁҕ > RxKotlin਷ RxJava হ੉ ز੘ೞ૑ ঋח׮. RxKotlin /** * disposable += observable.subscribe() */ operator fun CompositeDisposable.plusAssign(disposable: Disposable) { add(disposable) } fun Disposable.addTo(compositeDisposable: CompositeDisposable): Disposable = apply { compositeDisposable.add(this) } https://github.com/ReactiveX/RxKotlin

Slide 15

Slide 15 text

Android Schedulerܳ ನೣೞҊ ੓׮ RxJavaܳ ನೣೞҊ ੓׮ Rxjava ߡ੹ਸ ߹ب ݺदೞח Ѧ ୶ୌೞҊ ੓׮. RxAndroid Observable.just("one", "two", "three", "four", "five") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe {/* an Observer */} https://github.com/ReactiveX/RxAndroid

Slide 16

Slide 16 text

RxJava vs Coroutines

Slide 17

Slide 17 text

RxJava - Observable patternਵ۽ ҳة - ҳة റ ٜযয়ח dataܳ streams ഋక۽ ղ۰ࠁմ׮. - ઺р઺р ؘ੉ఠо ߸ജغח Ѧ Streamsਸ ా೧ ഛੋ оמೞ׮. - Coroutines - ־ҳա ইח ೣࣻܳ ഐ୹ೞҊ, return੉ য়ӝ ੹ө૑ ӝ׮ܽ׮. - ୊ܻо ՘ա return ೠ ؘ੉ఠܳ о૑Ҋ ׮਺ ୊ܻܳ ೡ ࣻ ੓׮. RxJava vs Coroutines

Slide 18

Slide 18 text

࠺زӝ ୊ܻ੄ ҙ੼ীࢲ ࠺Үೡ ࣽ ੓׮ ؘ੉ఠ੄ ൒ܴਸ ӝળਵ۽ ೠ׮ݶ ইפ׮. RxJava৬ Coroutinesਸ ࠺Үೞח ѱ ݏਸө? > Coroutines 1.3ী ୶оೠ Flow৬ ࠺Үೞח ѱ ખ ؊ ݏ׮

Slide 19

Slide 19 text

Flowח Coroutines 1.3ীࢲ ੿धਵ۽ ࢎਊ оמೞ׮ A cold asynchronous data stream that sequentially emits values and completes normally or with an exception. Flow fun call(): Flow = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value } } @Test fun runBlockingTest() = runBlocking { // Collect the flow foo().collect { value -> println(value) } } https://kotlinlang.org/docs/reference/coroutines/flow.html

Slide 20

Slide 20 text

GlobalScope.launch ࢤࢿ CoroutineScope().launch ࢤࢿ launch۽ Dispatcher ߸҃ withContext੄ ࢎਊ Flowীࢲ ೲਊೞ૑ ঋח ಁఢ fun foo(): Flow = flow { println("Flow started") for (i in 1..3) { delay(100) emit(i) coroutineScope { emit(100) } } } ੉ ҃਋ח ೲਊ೧ਃ

Slide 21

Slide 21 text

ࢠ೒ਸ ٜ݅যࠁ੗

Slide 22

Slide 22 text

दաܻয় E-Mailҗ passwordܳ ੑ۱߉ח׮ E-Mailҗ Passwordܳ ݽف ੑ۱߉ӝ ੹ীח ߡౡ ࠺ഝࢿച E-Mailҗ Password ઺ ೞա݅ ੑ۱غ঻ਵݶ ࠺ഝࢿച ݽٚ ੑ۱੉ ՘աݶ Login ߡౡ ഝࢿച ੑ۱ ੹ ੑ۱ റ

Slide 23

Slide 23 text

Subject ࢎਊ email/passwordܳ ߉ਸ BehaviorSubject ࢤࢿ email/password ݽف ߉ওਸ ٸ ୊ܻೡ ੉߮౟ ೞա ؊ ୶о RxJava۽ ҳഅ private val emailSubject = BehaviorSubject.create() private val passwordSubject = BehaviorSubject.create() private val enableLoginSubject = BehaviorSubject.create()

Slide 24

Slide 24 text

emailҗ password ੑ۱ਸ ؀ӝೡ combineLatest email/passwordо ੑ۱غ঻ਸ ٸ, isNotEmpty()ܳ ୓௼ೠ׮ ӒܻҊ Ӓ ୊ܻ ੿ࠁܳ enableLoginSubject۽ ֈѹળ׮. RxJava۽ ҳഅ Observable.combineLatest( emailSubject, passwordSubject, BiFunction { email, password -> email.isNotEmpty() && password.isNotEmpty() }) .subscribe { enableLoginSubject.onNext(it) }

Slide 25

Slide 25 text

enableLoginSubject੄ ਽׹ਸ Coroutinesਵ۽ ߉ইࠁ੗ Observer patternਸ return ഋక۽ ߉ਵ۰ݶ? ഛੋ਷ Coroutineਸ ഝਊೞ੗ public suspend inline fun suspendCancellableCoroutine( crossinline block: (CancellableContinuation) -> Unit ): T = suspendCoroutineUninterceptedOrReturn { uCont -> val cancellable = CancellableContinuationImpl(uCont.intercepted(), resumeMode = MODE_CANCELLABLE) block(cancellable) cancellable.getResult() } suspendCancellableCoroutine

Slide 26

Slide 26 text

ഛੋ਷ Coroutineਸ ഝਊೞ੗ @ExperimentalCoroutinesApi @InternalCoroutinesApi @Test fun test() = runBlocking { // ৈӝী ׸ח׮ } val ret = suspendCancellableCoroutine { continuation -> // ৈӝী RxJavaܳ ҳഅ continuation.invokeOnCancellation { try { cancel() } catch (e: Exception) { println(e) } } } assert(ret) enableLoginSubject .subscribeOn(Schedulers.io()) .subscribe({ continuation.resumeWith(kotlin.runCatching { it }) }, { continuation.resumeWithException(it) })

Slide 27

Slide 27 text

RxJava৬ Coroutineਸ ೣԋ ॳݶ? Coroutineਸ ੸ਊೞҊ र঻׮ݶ ب੹ оמ ݽف ߸҃ೞח Ѥ য۵Ҋ, Coroutineਸ ࢎਊ೧ࠁҊ र঻׮ݶ Coroutine੉ ݽٚ Ѧ ઁҕೞ૑ח ঋח׮. ੉۠ ۄ੉࠳۞ܻח https://github.com/Kotlin/kotlinx.coroutines/tree/ master/reactive/kotlinx-coroutines-rx2 ীоݶ ੓׮

Slide 28

Slide 28 text

Ӓۢ Coroutineਵ۽ ҳഅೞ۰ݶ? Coroutine੄ Message ୊ܻ ߑधਵ۽ ೧ࠅ ࣽ ੓׮. channels or actors channels : https://kotlinlang.org/docs/reference/ coroutines/channels.html actors : https://kotlinlang.org/docs/reference/ coroutines/shared-mutable-state-and- concurrency.html#actors

Slide 29

Slide 29 text

ೣ੿ਸ ౵ࢲ Coroutineਸ ݄ ॄࠁ੗

Slide 30

Slide 30 text

Android Unit Test - ೣ੿ਸ ٜ݅੗ fun call(): Flow = flow { for (i in 1..3) { delay(100) emit(i) // emit next value } } fun runBlockingTest() { GlobalScope.launch { launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } foo().collect { value -> println(value) } } } @Test fun callTest() { println("subroutines") runBlockingTest() // Coroutines println("subroutines") }

Slide 31

Slide 31 text

Android Unit Test - ખ ؊ ࠅө? fun call(): Flow = flow { for (i in 1..3) { delay(100) emit(i) // emit next value } } fun runBlockingTest() { GlobalScope.launch { launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } foo().collect { value -> println(value) } } } @Test fun callTest() { println("subroutines") runBlockingTest() // Coroutines println("subroutines") }

Slide 32

Slide 32 text

Android Unit Test - ੉ Ѿҗ ч਷ ަө? fun call(): Flow = flow { for (i in 1..3) { delay(100) emit(i) // emit next value } } fun runBlockingTest() { GlobalScope.launch { launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } foo().collect { value -> println(value) } } } @Test fun callTest() { println("subroutines") runBlockingTest() // Coroutines println("subroutines") } Ѿҗח Subroutines Subroutines ܳ ୹۱ೞҊ ՘դ׮.

Slide 33

Slide 33 text

Coroutine਷ ೣࣻ returnਸ ӝ׮ܽ׮. Coroutine block উীࢲ ݅ ৵? fun runBlockingTest() { GlobalScope.launch { // ৈӝח ௏ܖ౯ উ - ৈӝࢲח ௏ܖ౯ ੉߮౟ܳ ߉ਸ ࣻ ੓׮ } } @Test fun callTest() { // ৈӝח ௏ܖ౯੉ ইջ println("subroutines") runBlockingTest() // Coroutines println("subroutines") // ৈӝח ௏ܖ౯੉ ইջ - ௏ܖ౯ ੉߮౟ܳ ߉૑ ޅೠ׮ } Listener ١۾೮૑݅ Listener ߆ীח ৵ ੉߮౟о য়૑ ঋભ? > ߉ਸ Ҕ੉ ࢎۄ઎׮ > زੌೠ ߑߨ੉ۄݶ Coroutineب эইਃ

Slide 34

Slide 34 text

ޙࢲী ׹੉ ੓׮

Slide 35

Slide 35 text

Runs a new coroutine and blocks the current thread interruptibly until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests. runBlocking? https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html

Slide 36

Slide 36 text

೧Ѿ fun call(): Flow = flow { for (i in 1..3) { delay(100) emit(i) // emit next value } } fun runBlockingTest() { GlobalScope.launch { launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } foo().collect { value -> println(value) } } } @Test fun callTest() { println("subroutines") runBlockingTest() // Coroutines println("subroutines") } subroutines Flow started I'm not blocked 1 … 1 … 100 I'm not blocked 2 … 2 … 100 I'm not blocked 3 … 3 … 100 subroutines

Slide 37

Slide 37 text

Coroutine ࢎਊ द ঌইঠ ೡ Ѫ

Slide 38

Slide 38 text

runBlocking਷ Unit Testীࢲ ݅ ࢎਊ೧ঠ ೠ׮ Thread ઙܨܳ ૑੿ೞ૑ ঋওӝী UI Threadীࢲ ز੘ UIীࢲ runBlocking ࢎਊद ANR(Application Not Responding) ߊࢤ runBlocking਷ Unit testীࢲ ݅ public fun runBlocking( context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T { // ࢤۚ }

Slide 39

Slide 39 text

CoroutineScopeਸ ࢚ࣘ ߉ই ҳഅ೧ঠ ೠ׮. ૒੽ ҳഅदח lifecycleী ٮܲ cancel()ਸ ೧ঠೠ׮. AAC ViewModel/LiveDataਸ ࢎਊೡ ࣻ ੓׮ Androidীࢲ ੜ ࢎਊೞӝ class MyViewModel: ViewModel() { init { viewModelScope.launch { // Coroutine that will be canceled when the ViewModel is cleared. } } } val user: LiveData = liveData { val data = database.loadUser() // loadUser is a suspend function. emit(data) }

Slide 40

Slide 40 text

try/catchਸ ੉ਊೠ ী۞ ୊ܻ Error ୊ܻ launch { try { throw Exception("Error!!!!") } catch (e: Exception) { } }

Slide 41

Slide 41 text

CoroutineExceptionHandler ࢎਊ coroutineContext৬ throwable ੿ࠁܳ ੹׳ CoroutineScopeী Scheduler + exceptionHanderܳ ૑੿ ખ ؊ જ਷ Exception ୊ܻ val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> // Error ୊ܻ } CoroutineScope(exceptionHandler).launch { throw Exception("Error!!!!") }.invokeOnCompletion { if (ҕా੸ੋ য়ܨ ୊ܻ റী ژ ୊ܻо ೙ਃೞ׮ݶ?) { it?.printStackTrace() } }

Slide 42

Slide 42 text

Coroutines જਵפ ݋Ԉ ॳࣁਃ. Ҋ޹਷ ݆੉ ೙ਃ. RxJavaܳ ࠂ੟ೞѱ ॳח Ѫࠁ׮ח ੍ӝ ए਍ ௏٘ оةࢿ Androidীࢲח ԙ! lifecycleਸ ٮۄ઱ࣁਃ. 5.x؀੄ ࢖ࢿ ӝӝীࢲ ޙઁо ੓חؘ … Lifecycle ੜ ٮܰݶ ҡଳ਺ > Lifecycleਸ ҙܻೞӝ ൨ٜ׮ݶ LiveData৬ ೣԋ ॳݶ ௾ ޙઁ হ਺ > ੉ ޙઁח ݽٚ Threadীࢲ زੌೞѱ ߊࢤೞח ޙઁۄࢲ ௏ܖ౯ ݅੄ ޙઁח ইש Ӓېࢲ?

Slide 43

Slide 43 text

՘լযਃ! [email protected] https://thdev.tech