Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Reduce dependency on Rx with Kotlin Coroutines

2019/1/23に開催されたLINE Developer Meetup #49 in Kyotoでの登壇資料です

  • Identifiez-vous pour voir les commentaires

  • Soyez le premier à aimer ceci

Reduce dependency on Rx with Kotlin Coroutines

  1. 1. Reduce dependency on Rx with Kotlin Coroutines LINE Fukuoka @umakoz 2019 1 23 LINE Developer Meetup #49 in KYOTO
  2. 2. About me • LINE Fukuoka 3 • • @umakoz • LINE Creators Studio for Android • ! 3°02'03"
  3. 3. LINE Creators Studio • LINE • • https://creator.line.me/ja/studio/
  4. 4. inside LINE Creators Studio • Pure Kotlin without Library • MVVM • RxJava2, RxKotlin, RxLifecycle, Retrofit2, Realm...
  5. 5. Rx ReactiveX Reactive Extensions An API for asynchronous programming with observable streams.
  6. 6. Rx • Create Easily create event streams or data streams. • Combine Compose and transform streams with query-like operators. • Listen Subscribe to any observable stream to perform side effects.
  7. 7. Rx
  8. 8. RxJava A Java VM implementation of Reactive Extensions
  9. 9. RxJava event/data stream • Observable 0..N flows, no backpressure, • Flowable 0..N flows, supporting Reactive-Streams and backpressure
  10. 10. RxJava event/data stream • Single a flow of exactly 1 item or an error, • Completable a flow without items but only a completion or error signal, • Maybe a flow with no items, exactly one item or an error.
  11. 11. async code using RxJava2 fun loadSticker(id: Int): Single<Sticker> = ... loadSticker(id) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { sticker -> show(sticker) }, { error -> showError(error) } )
  12. 12. difficulty of Rx • • Functional Programming • Reactive Programming • operator http://reactivex.io/documentation/operators.html • Hot / Cold •
  13. 13. Kotlin 1.3 Released Coroutines
  14. 14. Coroutines : co-routine co https://ja.wikipedia.org/wiki/
  15. 15. async code using Coroutines suspend fun loadSticker(id: Int): Sticker = ... GlobalScope.launch(Dispatchers.Main) { try { val sticker = async(Dispatchers.IO) { loadSticker(id) }.await() show(sticker) } catch (error: Exception) { showError(error) } }
  16. 16. Coroutine scope A fundamental building block of coroutines. • threading • life-cycle object GlobalScope : CoroutineScope
  17. 17. Coroutine builder Starts coroutine from a regular non-suspending scope. fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit ): Job fun <T> CoroutineScope.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T ): Deferred<T>
  18. 18. Coroutine dispatcher Ensures that the execution of a coroutine gets dispatched on the right thread. • Dispatchers.Main • Dispatchers.Default • Dispatchers.IO • Dispatchers.Unconfined
  19. 19. suspend function Suspends execution of the code without blocking the current thread of execution. suspend fun loadSticker(id: Int): Sticker = ...
  20. 20. async code using Coroutines suspend fun loadSticker(id: Int): Sticker = ... GlobalScope.launch(Dispatchers.Main) { try { val sticker = async(Dispatchers.IO) { loadSticker(id) }.await() show(sticker) } catch (error: Exception) { showError(error) } }
  21. 21. Compare RxJava2 stream with Coroutines RxJava2 Coroutines Observable ReceiveChannel (experimental) Flowable ReceiveChannel (experimental) Single Deferred Completable Job Maybe Deferred
  22. 22. kotlinx-coroutines-rx2 • Modules that provide builders and iteration support for various reactive streams libraries: • Reactive Streams, RxJava 2.x, and Project Reactor https://github.com/Kotlin/kotlinx.coroutines/blob/master/ reactive/kotlinx-coroutines-rx2
  23. 23. RxJava2 to Coroutines suspend fun <T> SingleSource<T>.await(): T suspend fun CompletableSource.await(): Unit suspend fun <T> MaybeSource<T>.await(): T?
  24. 24. Coroutines to RxJava2 fun CoroutineScope.rxSingle(...): Single fun CoroutineScope.rxCompletable(...): Completable fun CoroutineScope.rxMaybe(...): Maybe
  25. 25. How to use Coroutines on MVVM
  26. 26. retrofit2-kotlin-coroutines-adapter A Retrofit 2 CallAdapter.Factory for RxJava2's Single. interface MyService { @GET("/sticker") fun loadSticker(id: Int): Single<Sticker> }
  27. 27. retrofit2-kotlin-coroutines-adapter A Retrofit 2 CallAdapter.Factory for Kotlin coroutine's Deferred. interface MyService { @GET("/sticker") fun loadSticker(id: Int): Deferred<Sticker> } https://github.com/JakeWharton/retrofit2-kotlin-coroutines- adapter
  28. 28. Conclusion • Coroutines • RxJava2 Coroutines • Coroutine Channel experimental • API DB Coroutines • View Rx Observable operator
  29. 29. Thank you

×